I know there are many control flow libraries for node.js. Some of them let one chain async functions with callbacks (like async, asyncblock, etc), the others use promise concept (Q, deferred, futures, etc). Given a long running script making a series of actions one after another that may fail at any time, which control flow would you prefer and why? What are the pros and cons?
Pros for callbacks:
(error,result)
callbacks throughout. I recommend following their argument order for consistency. (As opposed to say (result1, result2, result3, error)
.)Pros for promises:
.then(foo).then(bar)
calls.map
, Q has allResolved
, and ES6 Promises offer Promise.all()
. (This is also possible with callbacks, e.g. using async.parallel()
, but not built in.)if (err) return callback(err);
at the start of every callback.It would make sense to use callbacks near the bottom of the stack, for code which will be run many times per second. Higher up the stack, promises may be preferable as they are easier to read and understand, and can handle errors more elegantly.
It is worth noting that promises can be built from callbacks at runtime. So you can implement your core code in the minimalist callback form, and still expose a promises version of the library if you want to. (As in Q.nfbind()
.)
I would be interested to hear other pros/cons.
Bonus tip: Always handle errors! With both methods, if you do not handle the error then it will simply disappear, leaving you in the dark about why your code did not work as expected.
Callbacks should always handle
if (err) ...
and Promises should always have a.catch()
if they do not return.Even if you expect errors sometimes, and don't need to handle those, not handling unexpected errors means you won't hear about errors from developer mistakes such as typos, if the code is changed in future.
An alternative to
.catch()
for Promises is to listen for unhandled rejections. Personally I use this to issue a warning that.catch()
was missing!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With