【问题标题】:Promisification with Bluebird on not node-style callbacks在非节点式回调上使用 Bluebird 的承诺
【发布时间】:2016-09-12 13:53:24
【问题描述】:

我正在尝试在 https://github.com/seishun/node-steam-trade 上调用 Bluebird Promisificator,但该库使用的是非节点回调。

例如(Babel 的 ES6 语法):

import bluebird from 'bluebird';
import SteamTrade from 'steam-trade';

bluebird.promisifyAll(SteamTrade.prototype);

let steamTrade = new SteamTrade();

// some kind of set sessionid/cookies

let result = await steamTrade.openAsync('my-steam-id');

最后一行没有完成,因为第一个参数,传入回调是“数据”,而不是错误(docs)。

如何配置 bluebird 以处理来自第一个参数的数据?

【问题讨论】:

标签: javascript node.js promise bluebird


【解决方案1】:

你可能想使用 when.js (https://github.com/cujojs/when/blob/master/docs/api.md#whenlift)

或者您可以编写自己的 Promise 包装器。

steamTrade.openAsync = function(id){
  var promise = new bluebird( function(resolve, reject){
    steamTrade.open(id, function(data){ resolve(data); });
  });
  return promise;
};

【讨论】:

    猜你喜欢
    • 2014-06-23
    • 2016-07-27
    • 2016-02-02
    • 2018-02-06
    • 2018-02-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多