Promises are all the rage this year, so I tought I’d join the club and do a post about it.
Since promises are not a part of the core implementation of Node.js, callbacks are common pattern for the standard lib and for other libs that don’t have dependencies. In our case, promises prevented a lot of nesting, which would be much more difficult to avoid using pure callbacks.
We used RSVP.js as our promise implementation, and made a bunch of calls to the movie db API. Wrapping all those callbacks in promises became a chore, so I thought there should be a better way to do this. That’s when I decided to take a look at the source code of RSVP, and noticed the function “denodeify”.
Using request as an example, this handy function helps you transform this:
Much better! There is no documentation of this in the RSVP main page, but Q also has this and other utilities for adapting functions that take callbacks.