简体   繁体   English

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

[英]Results from Bluebird promisified library is returned as an error

I am currently using Bluebird with the fb npm package. 我目前正在将Bluebird与fb npm软件包一起使用。

I've managed to get the fb library to return data. 我设法使fb库返回数据。 However, the data is caught as an error instead of being passed to the then() method. 但是,数据被捕获为错误,而不是传递给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 }); 

Am I using promises in the wrong way? 我是否以错误的方式使用了诺言? So far I've attempted to follow the documentation on the Bluebird page. 到目前为止,我已经尝试遵循Bluebird页面上的文档。

The promisify function in bluebird, by default, expects the callback API to be: 缺省情况下,bluebird中的promisify函数期望回调API为:

  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: 查看npm上的fb包,我们可以看到回调使用以下形式:

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. 这意味着此API违反了规则2和3。 Luckily, bluebird allows users to write custom promisifier functions, see the bluebird API for details. 幸运的是,bluebird允许用户编写自定义的promisifier函数,有关详细信息,请参阅bluebird API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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