我正在尝试将此Braintree嵌套的回调转换为async / await:

  var stream = gateway.transaction.search(function (search) {
    search.customerId().is(braintreeCustomerId);
  }, function (err, response) {
    response.each(function (err, transaction) {
      console.log(transaction);
    });
  });


我尝试了这种方法,但是得到了undefined响应输出:

  await gateway.transaction.search(async (search) => {
    const response = await search.customerId().is(braintreeCustomerId);
    console.log(response)
  })


我究竟做错了什么?

最佳答案

async函数通常不应与未意识到诺言的API一起使用,因为这会导致未链接的诺言失控。

如果gateway.transaction.search不支持promise,则需要进行承诺:

const search = util.promisify(gateway.transaction.search).bind(gateway.transaction);

const searchResult = await search();
const response = searchResult.customerId().is(braintreeCustomerId);


如果searchResult.customerId().is(braintreeCustomerId)不返回承诺,则不需要await

关于javascript - 尝试将Braintree嵌套的回调转换为NodeJS async/await语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54476387/

10-15 05:58