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) {

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.

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

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
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos


  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!