也许我不了解Promise.promisifyAll的工作方式。我正在尝试分发coinbase包。

基本上,客户端的功能是有保证的,但是这些功能返回的帐户似乎没有异步版本:[TypeError: acc.getTransactionsAsync is not a function]

我已经尝试按照an answer to a similar question中的建议将{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应该与对象的属性一样,不会受到先前编辑的干扰。在这一点上,我认为可能没有别的选择,可以为我需要的所有功能制定自己的承诺,而这些功能并不直接存在于客户端之下。

最佳答案

我认为promisifyAll仅解析客户端功能,并且未在处理模型。


不完全是。这与promisifyAll遍历方法和属性的方式无关。相反,您显式地仅传递要被承诺的客户端方法:


var Client = require('coinbase').Client;
var client = new Client(…);
Promise.promisifyAll(client);



client到模型之间没有链接。而是尝试在整个模块上调用promisifyAll,而不仅是Client类:

var Client = Promise.promisifyAll(require('coinbase')).Client;


或者,如果这不起作用,请同时在promisifyAll类和Client类集合上调用models

var coinbase = require('coinbase');
var Client = Promise.promisifyAll(coinbase.Client);
Promise.promisifyAll(coinbase.model);

09-25 17:48