也许我不理解工作方式Promise.promisifyAll
。我正在尝试承诺coinbase包。
基本上,客户端的功能是承诺的,但这些功能返回的帐户似乎没有异步版本:[TypeError: acc.getTransactionsAsync is not a function]
.
我已经尝试按照类似问题的答案中的建议{multiArgs:true}
作为选项传递,但它并没有解决问题。任何建议表示赞赏。Promise.promisifyAll()
使用包的正常方式(作品):
var Client = require('coinbase').Client
var client = new Client({
'apiKey': '<snip>',
'apiSecret': '<snip>',
'baseApiUri': 'https://api.sandbox.coinbase.com/v2/',
'tokenUri': 'https://api.sandbox.coinbase.com/oauth/token'
});
//Callbacks
client.getAccounts({}, function(err, accounts) {
accounts.forEach(function(acc) {
acc.getTransactions(null, function(err, txns) {
txns.forEach(function(txn) {
console.log('txn: ' + txn.id);
});
});
});
});
承诺的版本不起作用(getTransactionsAsync
未定义,但getAccountsAsync
正确返回帐户):
var Promise = require('bluebird');
var Client = require('coinbase').Client;
var client = new Client({
'apiKey': '<snip>',
'apiSecret': '<snip>',
'baseApiUri': 'https://api.sandbox.coinbase.com/v2/',
'tokenUri': 'https://api.sandbox.coinbase.com/oauth/token'
});
Promise.promisifyAll(client);
//Promises
client.getAccountsAsync({}) //Works perfectly, returns the accounts
.then(function(accounts) {
return Promise.map(accounts, function(acc) {
return acc.getTransactionsAsync(null); //This function call is throwing the TypeError
});
})
.then(function(transactions) {
console.log('Transactions:');
transactions.forEach(function(tx) {
console.log(tx);
});
})
.catch(function(err) {
console.log(err);
});
编辑:
浏览我想要承诺的包,我意识到我试图调用的函数来自包返回的模型对象。我认为promisifyAll
只解析客户端函数,模型没有被处理。我只是不太了解如何进行解析:(
这是 index.js(导出的模块)
var Account = require('./lib/model/Account.js'),
Address = require('./lib/model/Address.js'),
Buy = require('./lib/model/Buy.js'),
Checkout = require('./lib/model/Checkout.js'),
Client = require('./lib/Client.js'),
Deposit = require('./lib/model/Deposit.js'),
Merchant = require('./lib/model/Merchant.js'),
Notification = require('./lib/model/Notification.js'),
Order = require('./lib/model/Order.js'),
PaymentMethod = require('./lib/model/PaymentMethod.js'),
Sell = require('./lib/model/Sell.js'),
Transaction = require('./lib/model/Transaction.js'),
User = require('./lib/model/User.js'),
Withdrawal = require('./lib/model/Withdrawal.js');
var model = {
'Account' : Account,
'Address' : Address,
'Buy' : Buy,
'Checkout' : Checkout,
'Deposit' : Deposit,
'Merchant' : Merchant,
'Notification' : Notification,
'Order' : Order,
'PaymentMethod' : PaymentMethod,
'Sell' : Sell,
'Transaction' : Transaction,
'User' : User,
'Withdrawal' : Withdrawal
};
module.exports = {
'Client' : Client,
'model' : model
};
编辑2:
客户端需要它自己的模型模块,因此Promise.promisifyAll
应该像预期的那样使用对象的属性,而不会干扰先前的编辑。在这一点上,我认为可能没有其他选择可以为我需要的所有功能做出自己的承诺,这些功能不直接存在于客户端下。