Search code examples
javascriptnode.jscallbackpromisebluebird

Node.js: Converting module functions from callbacks to promises with Bluebird


I have a simple module with a couple of functions to interact with an Active Directory.

Using the standard callback style, the code works just fine:

/**
* mylib.js
**/

const ActiveDirectory = require('activedirectory');

// Instantiate AD client
const ad = new ActiveDirectory({
  url: 'ldaps://...',
  baseDN: 'DC=...',
});

module.exports = {
  // Authenticate user against the AD
  authenticate: (username, password, callback) => {
    // Authentication request
    ad.authenticate(username, password, (err, status) => {
      callback(err, status);
    });
  },
};

/**
* client.js
**/

const mylib = require('./mylib');

mylib.authenticate('<username>', '<password>', (err, status) => {
  if (err) {
    console.log(`Error: ${err}`);
    return;
  }
  console.log(`Success: ${status}`);
}); 

Execution result:

> node client.js
Success: true

The next step was to refactor my lib method to use Promises instead of callbacks:

/**
* mylib.js
**/

const ActiveDirectory = require('activedirectory');
const Promise = require('bluebird');

//...

module.exports = {
  // Authenticate user against AD
  authenticate: Promise.method((username, password) => {
    ad.authenticate(username, password, (err, status) => {
      if (err) throw err;
      return status;
    });
  }),
};

/**
* client.js
**/

const mylib = require('./mylib');

myLib.authenticate('<username>', '<password>').then((status) => {
  console.log(`Success: ${status}`);
}).catch((err) => {
  console.log(`Error: ${err}`);
});

Execution result:

> node client.js
Success: undefined

So it looks like the status is not being resolved.

If I change the AD server URL to something else (to force a connection error and see the rejection) I can see both the resolve and the rejection log:

> node client.js
Success: undefined

/Users/.../mylib.js:84
      if (err) throw err;
               ^

Error: connect ECONNREFUSED <IP>
    at Object.exports._errnoException (util.js:1036:11)
    at exports._exceptionWithHostPort (util.js:1059:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)

What am I doing wrong? Can't figure it out.


Solution

  • You are expecting too much of Promise.method(), from which a thrown error or returned value will indeed be promise wrapped, but that's not what you are doing - you are throwing/returning from ad.authenticate's callback.

    What you want is a promisified version of ad.authenticate, which Bluebird makes extremely simple.

    module.exports = {
      // Authenticate user against AD
      authenticate: Promise.promisify(ad.authenticate)
    };