【发布时间】:2017-09-17 22:25:04
【问题描述】:
我有以下代码:
console.log("TEST 1");
accounts.forEach(async function(account) {
console.log("TEST 2");
try {
var data = await getReservations(account);
} catch(err) {
res.status(err.error_code).json(err);
}
console.log("TEST 3");
});
console.log("TEST 4");
当它运行时,控制台显示:
TEST 1
TEST 2
TEST 4
getReservations()
TEST 3
但我希望它等待getReservations() 完成,然后再继续通过forEach()。我希望它输出:
TEST 1
TEST 2
getReservations()
TEST 3
TEST 4
有什么想法吗?
【问题讨论】:
-
您的问题可能已经回答here。
-
这是一个 for 循环比
.forEach更好的地方 -
@Pyromonk - 这需要异步库而不是使用 async/await
-
@JaromandaX,谢谢。
标签: javascript promise