【发布时间】:2021-01-02 06:33:03
【问题描述】:
let array = [1,2,3];
array.forEach(function(item,index) {
var data = fetch('https://reqres.in/api/products/'+item);
//wait for response after response come then next iterating a loop (first,second,third)
});
我想跑步
- 第一次调用api
- 等待回复
- 显示成html
但目前我的脚本调用所有 3 次 api 而无需等待响应
我想等待响应,完成后调用下一个 api ..就像那样
【问题讨论】:
-
你可能想使用 async/await。 javascript.info/async-await
-
您应该使用
for of循环或普通for循环。异步/等待函数不能与 forEach 一起正常工作。
标签: javascript node.js angularjs promise async-await