3 ms·
I actually just wrote a Node + Express + Backbone error handling system today. I've been staying sane through the use of the 'Q' promise package (https://githu
by PhrosTT 14y ago
I actually just wrote a Node + Express + Backbone error handling system today.
I've been staying sane through the use of the 'Q' promise package (https://github.com/kriskowal/q https://github.com/kriskowal/q). For example, in the below code chunk any errors thrown in makeEmailConfirmation or sendEmailConfirmation will land in the .fail() catch block.
I believe using a promise system is the only way to avoid callback hell.
var deferred = Q.defer();
...
Storage.CustomLinks.makeEmailConfirmationLink(user)
.then(function(link) {
return Email.sendEmailConfirmation(user, link);
})
.then(function(result) {
deferred.resolve(result);
})
.fail(function(err) {
deferred.reject(new ServerError(err));
})
.end();
...
return deferred.promise;
- beefsack 14y agoI've recently written an application extensively using Q, and was very pleased at how clean my code ended up being. Particularly in comparison to how node.js code tends to end up looking once your callback pyramids start to grow.
- olegp 14y agoYou can also use fibers and make the code appear synchronous, which gives you the ability to use exceptions for error handling.
- rogerhcastillo 14y ago+1 for promises based exception handling. I have used Q for a number of applications. Q really simplifies error handling and by chaining error propagation forward to asynchronous result consumers.