try.catch

1.1.1 • Public • Published

try.catch Build Status

Simple performant try-catch wrapper.

This utility is used in place of regular try/catch blocks using a clean functional syntax. Execution is fully synchronous; this does not work with async methods

Instead of this

 
try {
    // do something
}
catch (err) {
    // handle errors
}
finally {
    // finally do something
}
 

Do this

 
var Try = require('try.catch');
 
Try(function () {
    // do something
})
.catch(function (err) {
    // handle errors
})
.finally(function () {
    // finally do something
});
 

It can also be used like this:

 
function doSomething (x, y) {
    return this + x * y;
}
 
var result = Try(doSomething, 'test', 2, 3)     // pass context and arguments to function
.catch(function (err) {
    // err was thrown
})                                              // chain catch blocks
.catch(TypeError, function (err) {              // catch errors conditionally
    // TypeError was thrown
})
.finally(function (value, err) {
    // finally do something
});
 
console.log(result.value); // return value of function if any
console.log(result.error); // error thrown if any
 

See tests for full spec


Tests

Run tests using npm test

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.1
    8
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.1.1
    8
  • 1.0.0
    0

Package Sidebar

Install

npm i try.catch

Weekly Downloads

8

Version

1.1.1

License

MIT

Last publish

Collaborators

  • rydm