javascript - Bluebird promisified 库的结果作为错误返回

标签 javascript node.js facebook-javascript-sdk bluebird

我目前正在使用 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 为:

  1. 要 promise 的函数的最后一个参数是回调函数
  2. 回调函数第一个参数是错误值
  3. 回调函数的第二个参数是结果值

查看npm上的fb包,我们可以看到回调使用以下形式:

函数(res){ ...}

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

关于javascript - Bluebird promisified 库的结果作为错误返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32078168/

相关文章:

javascript - MongoDB 获取最后一个元素不能与 node.js 一起正常工作

javascript - 无法响应从 express 发送的自定义错误消息

javascript - Facebook Connect Javascript SDK 突然无法在 Chrome 和 Firefox 中加载

javascript - Facebook 发送对话框链接问题

javascript - MVC4,使用 jQuery 在对话框中呈现局部 View ,如何刷新

javascript - 注入(inject) Controller 的自更新服务

node.js - Mongoose findById 未找到结果时不会返回 null

javascript - 由 `ref` 调用的 React input.focus() 也调用 `onBlur` 事件

javascript - 在输入 [type ='number' ] Edge 总是为 `NaN` 返回 `valueAsNumber`

ios - IOS FB.init上的PhoneGap/Cordova 2.2.0 Facebook插件失败