dezonkey
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
function delay (time, cb) {
if (typeof time !== 'number') {
throw new Error(time + ' is not a number')
}
setTimeout(cb, time)
}
delay('123', cb)
function onError(err) {
...
}
try {
delay('123', function (err) {
if (err) onError(err)
})
} catch (err) {
onError(err)
}
var safeDelay = dezonkey(delay)
safeDelay('123', cb)
function myFunction (time, cb) {
return dezonkey(function () {
if (typeof time !== 'number') {
throw new Error(time + ' is not a number')
}
setTimeout(cb, time)
})
}
Example
See example.js
Benchmark
See benchmark
Test
npm install standard
npm test