gpt4 book ai didi

node.js - NodeJS : How to wait for the HTTP Get request is complete in For Loop?

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:07 25 4
gpt4 key购买 nike

我在 NodeJS 中有一个 for 循环函数。我想等到For Loop中Http Get请求的结果完成后再执行下一次迭代,如何实现?

for (let k=0; k<fd.length; k++) {
url = fd[k].nct_id;

HttpSearch({condition: url}).then(trials => {
//Get the result first before execute the next iteration
console.log(trials);
});
}

最佳答案

您应该使 for 循环异步:

const main = async () => {
for (let k = 0; k < fd.length; k++) {
const url = fd[k].nct_id;

const trials = await HttpSearch({ condition: url });

console.log(trials);
}
};

main().catch(console.error);

这将导致循环在每个 HttpSearch 处“暂停”。

关于node.js - NodeJS : How to wait for the HTTP Get request is complete in For Loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52290767/

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