我目前正在使用 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
});
我是否以错误的方式使用了 Promise?到目前为止,我已尝试遵循 Bluebird 页面上的文档。
最佳答案
默认情况下,bluebird 中的 promisify
函数期望回调 API 为:
- 要 promise 的函数的最后一个参数是回调函数
- 回调函数第一个参数是错误值
- 回调函数的第二个参数是结果值
查看npm上的fb
包,我们可以看到回调使用以下形式:
函数(res){ ...}
其中回调函数的第一个参数是结果,并且似乎没有错误值的参数。这意味着该 API 违反了规则#2 和#3。幸运的是,bluebird 允许用户编写自定义 promisifier 函数,请参阅 bluebird API了解详情。
关于javascript - Bluebird promisified 库的结果作为错误返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32078168/