title: how to use bluebird to promisify node-rest-client
body: <p>I'm trying to promisify the node-rest-client library (tried restler but seems not to be re-entrant and caused problems with parallel POSTs to same endpoint).</p>
  
  <p>I've tried to use a similar approach to the provided filter/promisifer shown in the bluebird docs for restler, however can't seem to get it working.</p>
  
  <p>node-rest-client uses a combination of callback function with two arguments for successful response whilst also providing an event emitter for timeouts and errors.</p>
  
  <pre><code>var Promise = require("bluebird");
  var methodNamesToPromisify = "get post put del patch".split(" ");
  
  function EventEmitterPromisifier(originalMethod) {
      // return a function
      return function promisified() {
          var args = [].slice.call(arguments);
          var self = this;
          return new Promise(function(resolve, reject) {
              var emitter = originalMethod.apply(self, args, function(data, response){
                resolve([data, response]);
              });
  
              emitter
                  .on("error", function(err) {
                      reject(err);
                  })
                  .on("requestTimeout", function() {
                      reject(new Promise.TimeoutError());
                  })
                  .on("responseTimeout", function() {
                      reject(new Promise.TimeoutError());
                  });
          });
      };
  };
  
  exports.promisifyClient = function(restClient){
  
    Promise.promisifyAll(restClient, {
        filter: function(name) {
            return methodNamesToPromisify.indexOf(name) &gt; -1;
        },
        promisifier: EventEmitterPromisifier
    });
  }
  </code></pre>
  
  <p>and elsewhere in the code:</p>
  
  <pre><code>var Client = require('node-rest-client').Client;
  
  var constants = require('../../config/local.env');
  
  var restClient = new Client();
  promisifyClient(restClient);
  </code></pre>
  
  <p>which successfully adds promisified functions to <code>restClient</code></p>
  
  <p>However, when I call <code>postAsync(url, options).then(...)</code> the node-rest-client library throws an error saying the callback is undefined.</p>
  
  <p>As far s I can see this should work, but it seems the callback function provided in the promisifier isn't making it through to the library.</p>
  
  <p>I'm hoping someone with more experience in Bluebird might be able to see what I'm doing wrong.</p>