node.js - NodeJS:如何在 For Loop 中等待 HTTP Get 请求完成?
问题描述
我在 NodeJS 中有一个 for 循环函数。我想等到 Http Get 请求的结果在 For Loop 中完成后再执行下一次迭代,我该如何实现呢?
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循环async
:
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
.
推荐阅读
- ruby - Ruby 脚本查找方法返回 nil 类?
- r - In an R data frame, for a given row, how can I find what percentage a value in column A is of a value in column B?
- mongodb - 构建动态 MongoDB 查询以更新 dict 中的嵌套值
- powershell - 如何按文件路径搜索软件,然后在缺少时使用 PowerShell 安装它
- angular - Angular custom form validation message not showing up on 1 specific field
- chainer - 将 pretrain caffe 模型导入到 chainer 时出现错误解析消息
- c# - WPF:两台显示器上的同步视频渲染缺乏
- javascript - Slidein element is visible before the animation
- sql-server - Laravel table::create sqlserver sql server 上的日期转换
- azure-api-management - API 管理服务自定义域证书