Kiang Teng Kiang Teng - 6 months ago 22
Node.js Question

Results from Bluebird promisified library is returned as an error

I am currently using Bluebird with the fb npm package.

I've managed to get the fb library to return data. However, the data is caught as an error instead of being passed to the

then()
method.



var Promise = require('bluebird'),
fb = Promise.promisifyAll(require('fb'));

fb.apiAsync(endPoint, options)
.then(function(response) {
console.log(response); // This doesn't get called
}, function(e) {
console.log(e); // The facebook response gets returns as part of the error instead
});





Am I using promises in the wrong way? So far I've attempted to follow the documentation on the Bluebird page.

Answer

The promisify function in bluebird, by default, expects the callback API to be:

  1. Last parameter of the function to be promisified is the callback function
  2. The first parameter of the callback function is the error value
  3. The second parameter of the callback function is the result value

Looking at the fb package on npm, we can see that the callback uses the form:

function (res) { ...}

Where the first parameter of the callback function is the result, and there appears to be no parameter for the error value. This means that this API violates rules #2 and #3. Luckily, bluebird allows users to write custom promisifier functions, see the bluebird API for details.

Comments