├── .gitignore ├── README.md ├── package.json ├── test └── test.js └── tryit.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tryit 2 | 3 | Tiny module wrapping try/catch in JavaScript. 4 | 5 | It's *literally 11 lines of code*, [just read it](tryit.js) that's all the documentation you'll need. 6 | 7 | 8 | ## install 9 | 10 | ``` 11 | npm install tryit 12 | ``` 13 | 14 | ## usage 15 | 16 | What you'd normally do: 17 | ```js 18 | try { 19 | dangerousThing(); 20 | } catch (e) { 21 | console.log('something'); 22 | } 23 | ``` 24 | 25 | With try-it (all it does is wrap try-catch) 26 | ```js 27 | var tryit = require('tryit'); 28 | 29 | tryit(dangerousThing); 30 | ``` 31 | 32 | You can also handle the error by passing a second function 33 | ```js 34 | tryit(dangerousThing, function (e) { 35 | if (e) { 36 | console.log('do something'); 37 | } 38 | }) 39 | ``` 40 | 41 | 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. 42 | 43 | ## WHAT? WHY DO THIS!? 44 | 45 | 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. 46 | 47 | 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. 48 | 49 | Obviously, this is useful any time you're going to attempt to read some unknown data structure. 50 | 51 | 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. 52 | 53 | 54 | ## license 55 | 56 | [MIT](http://mit.joreteg.com/) 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tryit", 3 | "description": "Module to wrap try-catch for better performance and cleaner API.", 4 | "version": "1.0.3", 5 | "author": "Henrik Joreteg ", 6 | "files": [ 7 | "tryit.js" 8 | ], 9 | "devDependencies": { 10 | "tap-spec": "^2.1.2", 11 | "tape": "^3.0.3" 12 | }, 13 | "keywords": [ 14 | "errors", 15 | "try", 16 | "errorhandling" 17 | ], 18 | "license": "MIT", 19 | "main": "tryit.js", 20 | "repository": { 21 | "type": "git", 22 | "url": "git@github.com:HenrikJoreteg/tryit.git" 23 | }, 24 | "scripts": { 25 | "test": "node test/test.js | tap-spec" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var tryit = require('../tryit'); 3 | 4 | 5 | test('basic functionality', function (t) { 6 | var count = 0; 7 | 8 | var noOp = function () {}; 9 | var throwsError = function () { 10 | throw new Error('whammo'); 11 | } 12 | 13 | tryit(noOp, function (e) { 14 | t.ok(e == null, 'should be called without an error'); 15 | }); 16 | 17 | tryit(throwsError, function (e) { 18 | t.ok('should be called'); 19 | t.ok(e instanceof Error); 20 | }); 21 | 22 | t.end(); 23 | }); 24 | 25 | test('handle case where callback throws', function (t) { 26 | var count = 0; 27 | 28 | t.throws(function () { 29 | tryit(function () {}, function(e) { 30 | count++; 31 | t.equal(count, 1, 'should be called once'); 32 | throw new Error('kablowie'); 33 | }); 34 | }, 'should throw once'); 35 | 36 | t.end(); 37 | }); 38 | -------------------------------------------------------------------------------- /tryit.js: -------------------------------------------------------------------------------- 1 | // tryit 2 | // Simple, re-usuable try-catch, this is a performance optimization 3 | // and provides a cleaner API. 4 | module.exports = function (fn, cb) { 5 | var err; 6 | 7 | try { 8 | fn(); 9 | } catch (e) { 10 | err = e; 11 | } 12 | 13 | if (cb) cb(err || null); 14 | }; 15 | --------------------------------------------------------------------------------