doAsyncCall(function onSuccess(result) {
// do things
}, function onError(err) {
// No need to check, it's really an error.
});
This is much cleaner and lets you reuse error handling functions. doAsyncCall(function onSuccess(result) {
// do things
}, function onError(err) {
// No need to check, it's really an error.
});
This is much cleaner and lets you reuse error handling functions.In the two-callback model, the most common way to reify the handler pair is with a {success:...,error:...} object. This is much better than function(x,y,z,success,error), but still has the problem that you now need to override two handlers if you want to wrap some completion logic, regardless of success/failure. For example, if you want to close a file handle, win or lose, you need to provide two new functions.
Composition greatly benefits from a singular object. The Node.js approach is the (err,data) callback. It's an extremely simple and successful approach. The other approach are promises/futures, which internally can be implemented with one or more callbacks, but provide a nice clean interface for composition. You could get that with success/error objects, but you're exposing the internals rather than an interface, like you should be doing. A good promise interface would let you choose an implementation without tying you to any particular callback model.