简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM