Bluebird承诺库的结果作为错误返回

Results from Bluebird promisified library is returned as an error

本文关键字:错误 返回 结果 承诺 Bluebird      更新时间:2023-09-26

我目前正在使用Bluebird和fb npm包。

我已经设法让fb库返回数据。但是,数据被捕获为错误,而不是传递给then()方法。

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
  });

我用错误的方式使用承诺吗?到目前为止,我一直尝试遵循Bluebird页面上的文档。

bluebird中的promisify函数,默认情况下,期望回调API为:

  1. 承诺函数的最后一个参数为回调函数
  2. 回调函数的第一个参数是错误值
  3. 回调函数的第二个参数是结果值
查看npm上的fb包,我们可以看到回调使用的形式是:

function (res) { ...}

其中回调函数的第一个参数是结果,并且似乎没有错误值的参数。这意味着这个API违反了规则#2和#3。幸运的是,bluebird允许用户编写自定义承诺函数,详细信息请参阅bluebird API。