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
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • 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

  • By
    Create a Dojo Lightbox with dojox.image.Lightbox

    One of the reasons I love the Dojo Toolkit is that it seems to have everything.  No scouring for a plugin from this site and then another plugin from that site to build my application.  Buried within the expansive dojox namespace of Dojo is

  • By
    background-size Matters

    It's something that makes all men live in fear, and are often uncertain of. It's never spoken, but the curiosity is always there. Nine out of ten women agree in the affirmative. Advertisers do their best to make us feel inadequate but...

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!