updated sys and created publi
This commit is contained in:
+56
@@ -0,0 +1,56 @@
|
||||
# tryit
|
||||
|
||||
Tiny module wrapping try/catch in JavaScript.
|
||||
|
||||
It's *literally 11 lines of code*, [just read it](tryit.js) that's all the documentation you'll need.
|
||||
|
||||
|
||||
## install
|
||||
|
||||
```
|
||||
npm install tryit
|
||||
```
|
||||
|
||||
## usage
|
||||
|
||||
What you'd normally do:
|
||||
```js
|
||||
try {
|
||||
dangerousThing();
|
||||
} catch (e) {
|
||||
console.log('something');
|
||||
}
|
||||
```
|
||||
|
||||
With try-it (all it does is wrap try-catch)
|
||||
```js
|
||||
var tryit = require('tryit');
|
||||
|
||||
tryit(dangerousThing);
|
||||
```
|
||||
|
||||
You can also handle the error by passing a second function
|
||||
```js
|
||||
tryit(dangerousThing, function (e) {
|
||||
if (e) {
|
||||
console.log('do something');
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
The second function follows error-first pattern common in node. So if you pass a callback it gets called in both cases. But will have an error as the first argument if it fails.
|
||||
|
||||
## WHAT? WHY DO THIS!?
|
||||
|
||||
Primary motivation is having a clean way to wrap things that might fail, where I don't care if it fails. I just want to try it.
|
||||
|
||||
This includes stuff like blindly reading/parsing stuff from localStorage in the browser. If it's not there or if parsing it fails, that's fine. But I don't want to leave a bunch of empty `catch (e) {}` blocks in the code.
|
||||
|
||||
Obviously, this is useful any time you're going to attempt to read some unknown data structure.
|
||||
|
||||
In addition, my understanding is that it's hard for JS engines to optimize code in try blocks. By actually passing the code to be executed into a re-used try block, we can avoid having to have more than a single try block in our app. Again, this is not a primary motivation, just a potential side benefit.
|
||||
|
||||
|
||||
## license
|
||||
|
||||
[MIT](http://mit.joreteg.com/)
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
{
|
||||
"raw": "tryit@^1.0.1",
|
||||
"scope": null,
|
||||
"escapedName": "tryit",
|
||||
"name": "tryit",
|
||||
"rawSpec": "^1.0.1",
|
||||
"spec": ">=1.0.1 <2.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"/mnt/Data/bach/Documents/ola/OLA#3DOC/sys/node_modules/gulp-wrap"
|
||||
]
|
||||
],
|
||||
"_from": "tryit@>=1.0.1 <2.0.0",
|
||||
"_id": "tryit@1.0.3",
|
||||
"_inCache": true,
|
||||
"_location": "/tryit",
|
||||
"_nodeVersion": "6.8.1",
|
||||
"_npmOperationalInternal": {
|
||||
"host": "packages-12-west.internal.npmjs.com",
|
||||
"tmp": "tmp/tryit-1.0.3.tgz_1477606530482_0.8131801665294915"
|
||||
},
|
||||
"_npmUser": {
|
||||
"name": "henrikjoreteg",
|
||||
"email": "henrik@joreteg.com"
|
||||
},
|
||||
"_npmVersion": "3.10.8",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"raw": "tryit@^1.0.1",
|
||||
"scope": null,
|
||||
"escapedName": "tryit",
|
||||
"name": "tryit",
|
||||
"rawSpec": "^1.0.1",
|
||||
"spec": ">=1.0.1 <2.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/gulp-wrap"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz",
|
||||
"_shasum": "393be730a9446fd1ead6da59a014308f36c289cb",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "tryit@^1.0.1",
|
||||
"_where": "/mnt/Data/bach/Documents/ola/OLA#3DOC/sys/node_modules/gulp-wrap",
|
||||
"author": {
|
||||
"name": "Henrik Joreteg",
|
||||
"email": "henrik@andyet.net"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/HenrikJoreteg/tryit/issues"
|
||||
},
|
||||
"dependencies": {},
|
||||
"description": "Module to wrap try-catch for better performance and cleaner API.",
|
||||
"devDependencies": {
|
||||
"tap-spec": "^2.1.2",
|
||||
"tape": "^3.0.3"
|
||||
},
|
||||
"directories": {},
|
||||
"dist": {
|
||||
"shasum": "393be730a9446fd1ead6da59a014308f36c289cb",
|
||||
"tarball": "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz"
|
||||
},
|
||||
"files": [
|
||||
"tryit.js"
|
||||
],
|
||||
"gitHead": "706147151922a456988a641b08984b2d1fcf2a86",
|
||||
"homepage": "https://github.com/HenrikJoreteg/tryit#readme",
|
||||
"keywords": [
|
||||
"errors",
|
||||
"try",
|
||||
"errorhandling"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "tryit.js",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "henrikjoreteg",
|
||||
"email": "henrik@andyet.net"
|
||||
}
|
||||
],
|
||||
"name": "tryit",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/HenrikJoreteg/tryit.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test/test.js | tap-spec"
|
||||
},
|
||||
"version": "1.0.3"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// tryit
|
||||
// Simple, re-usuable try-catch, this is a performance optimization
|
||||
// and provides a cleaner API.
|
||||
module.exports = function (fn, cb) {
|
||||
var err;
|
||||
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
if (cb) cb(err || null);
|
||||
};
|
||||
Reference in New Issue
Block a user