Skip to content

Instantly share code, notes, and snippets.

@MNBuyskih
Created June 1, 2015 11:11
Show Gist options
  • Save MNBuyskih/2595b17f3861d70fd584 to your computer and use it in GitHub Desktop.
Save MNBuyskih/2595b17f3861d70fd584 to your computer and use it in GitHub Desktop.
Quick error handling function
function fail(errorCb, successCb, context) {
return function () {
var args = Array.prototype.slice.call(arguments),
isError = !!args[0];
((isError ? errorCb : successCb) || function () {
return arguments;
}).apply(context || null, isError ? args : args.splice(1));
};
}
@MNBuyskih
Copy link
Author

Usage

fs.readDir('/tmp', fail(function (error) {
    console.log(error);
}, function (files) {
    console.log(files);
}));

Predefined error handling

function myFail(done) {
    return fail(function (error) {
        throw error;
    }, done);
}
fs.readDir('/tmp', myFail(function (files) {
    console.log(files);
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment