node.js callback tools
Tools for simplifying node.js callback style programming.
errh(func, cb)
Returns function(err, result) that calls cb(err) if err != null and else calls func(result, cb).
Simplifies this case:
function (cb) {
someAsyncCall(function (err, result) {
if (err != null) {
cb(err);
}
else {
cb(null, doSomethingWithResult(result));
} // too many lines of code
});
};
to this one:
var errh = require('ncbt').errh;
function (cb) {
someAsyncCall(errh(function (result, cb) {
cb(null, doSomethingWithResult(result)); // much nicer
}, cb);
};
errh.noResult(cb)
Returns function(err) that calls cb(err) shrinking any other arguments.
Useful for getting rid of insert or update results when querying db.
errh.nullResult(cb)
Like errh.noResult(cb), but calls cb(err, null).
License
MIT