cbrun

cbrun - run callbacks with validation

npm install cbrun
18 downloads in the last week
18 downloads in the last month

cbrun

A very tiny library to invoke callbacks

To install simply run:

npm install cbrun

This lib implements only one function. You can pass array of errors, what you pass to nested callbacks. On second argument you must put callback reference, third and further - it is arguments of callback, excluding error.

function callbackHandler(errors[], callback, [arg_0, arg_1, ...]);

Or you can put one error, but do not forget put callback anyway.

function callbackHandler(error, callback, [arg_0, arg_1, ...]);

This function exports to module, so you need to require it first:

var callbackHandler = require('cbrun');
var fs = require('fs');

//Our function, what invokes callback
function readFiles(callback) {
  fs.readFile('file1', function (err, data1) {
    fs.readFile('file2', function (err1, data2) {
        //You dont need to check error in nested callbacks, only on endpoint
        callbackHandler([err, err1], callback, data1, data2); 
    });
  });
}


//Usage of this function
readFiles(function (err, fileData1, fileData2) {
  if (err) {
    console.log(err);
    process.exit(1);
  }
  console.log(fileData1.toString(), fileData2.toString());
});

If you find bug, please open issue. Thanks ;)

npm loves you