Denodeify

Promises are all the rage this year, so I tought I’d join the club and do a post about it.

Last week I teamed up with some friends for the Node Knockout 2013 (with a great submission, by the way) and used a LOT of promises on the server side.

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:

into 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.