1.0.0 • Published 8 years ago

dezonkey v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
8 years ago

dezonkey

Build Status js-standard-style

dezonkey forbids callback style functions from throwing. It'll catch the exception and pass it to the callback instead.

Getting started

npm install dezonkey


var dezonkey = require('dezonkey');

or

<script src="node_modules/dezonkey/index.js"></script>
var dezonkey = window.dezonkey

Usage

// here is a an example of a zonkey function
function delay (time, cb) {
  if (typeof time !== 'number') {
    throw new Error(time + ' is not a number')
  }
  setTimeout(cb, time)
}

// what if the first argument is user input and we forgot a check? process will crash
delay('123', cb) // throws
// what if a user/program repeatedly sends that value?
// process will repeatedly crash

// we could do that
function onError(err) {
  ...
}
try {
  delay('123', function (err) {
    if (err) onError(err)
  })
} catch (err) {
  onError(err)
}

// but here is a nicer solution
var safeDelay = dezonkey(delay)
safeDelay('123', cb) // will not throw, passes the exception to the callback

// dezonkey is also pretty useful if you're writing your own functions
function myFunction (time, cb) {
  return dezonkey(function () {
    if (typeof time !== 'number') {
      // instead of `return cb(new Error(...))`
      // who never forgot that return keyword?
      throw new Error(time + ' is not a number')
    }
    setTimeout(cb, time)
  })
}

Example

See example.js

Benchmark

See benchmark

Test

npm install standard
npm test