gpt4 book ai didi

javascript - Bluebird 在非 Node 式回调上的 promise

转载 作者:行者123 更新时间:2023-11-28 06:01:12 25 4
gpt4 key购买 nike

我正在尝试调用 Bluebird promisificator https://github.com/seishun/node-steam-trade ,但该库使用非 Node 回调。

例如(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 来处理第一个参数中的数据?

最佳答案

您可能想使用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;
};

关于javascript - Bluebird 在非 Node 式回调上的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com