pax_global_header00006660000000000000000000000064130044761750014521gustar00rootroot0000000000000052 comment=706147151922a456988a641b08984b2d1fcf2a86 tryit-1.0.3/000077500000000000000000000000001300447617500126755ustar00rootroot00000000000000tryit-1.0.3/.gitignore000066400000000000000000000000271300447617500146640ustar00rootroot00000000000000.DS_Store node_modules tryit-1.0.3/README.md000066400000000000000000000031011300447617500141470ustar00rootroot00000000000000# 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/) tryit-1.0.3/package.json000066400000000000000000000010501300447617500151570ustar00rootroot00000000000000{ "name": "tryit", "description": "Module to wrap try-catch for better performance and cleaner API.", "version": "1.0.3", "author": "Henrik Joreteg ", "files": [ "tryit.js" ], "devDependencies": { "tap-spec": "^2.1.2", "tape": "^3.0.3" }, "keywords": [ "errors", "try", "errorhandling" ], "license": "MIT", "main": "tryit.js", "repository": { "type": "git", "url": "git@github.com:HenrikJoreteg/tryit.git" }, "scripts": { "test": "node test/test.js | tap-spec" } } tryit-1.0.3/test/000077500000000000000000000000001300447617500136545ustar00rootroot00000000000000tryit-1.0.3/test/test.js000066400000000000000000000014331300447617500151720ustar00rootroot00000000000000var test = require('tape'); var tryit = require('../tryit'); test('basic functionality', function (t) { var count = 0; var noOp = function () {}; var throwsError = function () { throw new Error('whammo'); } tryit(noOp, function (e) { t.ok(e == null, 'should be called without an error'); }); tryit(throwsError, function (e) { t.ok('should be called'); t.ok(e instanceof Error); }); t.end(); }); test('handle case where callback throws', function (t) { var count = 0; t.throws(function () { tryit(function () {}, function(e) { count++; t.equal(count, 1, 'should be called once'); throw new Error('kablowie'); }); }, 'should throw once'); t.end(); }); tryit-1.0.3/tryit.js000066400000000000000000000004011300447617500144010ustar00rootroot00000000000000// 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); };