1.0.1 • Published 9 years ago

no-throw v1.0.1

Weekly downloads
8
License
-
Repository
github
Last release
9 years ago

no-throw

NPM version Build Status Dependency Status Coverage percentage

Wraps a function and makes it return {error, value} instead of throwing.

Install

$ npm install --save no-throw

Usage

'use strict';

var noThrow = require('no-throw');

var sqrt = function(n) {
  if (n < 0) {
    throw new Error('Encountered a complication.');
  }

  return Math.sqrt(n);
};

console.log(
  noThrow(sqrt)( -1), // { error: <Error...>, value: undefined }
  noThrow(sqrt)(100)  // { error:  undefined, value:        10 }
);

License

MIT © Andrew Morris