Use Promises Instead of Callbacks with promisify-node

By  on  

One of the reasons we love promises so much is because they allows us to avoid the infamous callback hell that we've all experienced in these early days of Node.js.  When I see an API that doesn't use the promise pattern, I get annoyed.  Luckily I've found promisify-node, a module that wraps functions or objects in a promise wrapper so you can avoid the callback mess!

There are a few different ways to use promisify-node.  The first is wrapping a single function in the promise:

var promisify = require('promisify-node');

function async(callback) {
  callback(null, true);
}

// Convert the function to return a Promise.
var wrap = promisify(async);

// Invoke the newly wrapped function.
wrap().then(function(value) {
  console.log(value === true);
});

You could even recursively wrap a Node.js module's functions:

var promisify = require('promisify-node');
var fs = promisify('fs');

// This function has been identified as an asynchronous function so it has
// been automatically wrapped.
fs.readFile('/etc/passwd').then(function(contents) {
  console.log(contents);
});

And then you can wrap an object's methods:

var promisify = require('promisify-node');

var myObj = {
  myMethod: function(a, b, cb) {
    cb(a, b);
  }
};

// No need to return anything as the methods will be replaced on the object.
promisify(myObj);

// Intentionally cause a failure by passing an object and inspect the message.
myObj.myMethod({ msg: 'Failure!' }, null).then(null, function(err) {
  console.log(err.msg);
});

Since many front-end APIs are moving to Promise-based APIs, it would be awesome to use something like Promisify to get into the habit of using them on both the server and client sides. Be warned, however, that this module uses a snippet of code to detect function arguments. If you don't use a frequently-used callback argument name, like callback or cb, the promisify-wrapped function may not work correctly.

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

Discussion

  1. John Szwaronek

    Since you suggested using this on the front-end, it should be noted that production front-end code is often minified. Minification will change the variable names of the callback params. I’m posting this to prevent someone from doing a bunch of work and then hitting a gotcha going to production.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!