gpt4 book ai didi

javascript - 尝试将 Braintree 嵌套回调转换为 NodeJS 异步/等待语法

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

我正在尝试将此 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 函数通常不应该与不知道 Promise 的 API 一起使用,因为这会导致未链接的失控 Promise。

如果gateway.transaction.search不支持 promise ,则需要对其进行 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) 未返回 Promise,则不需要对其进行 await 编辑。

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

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