asynchronous - for of 循环甚至在 async/await 甚至在响应之前继续
问题描述
下面是我的代码片段,其中包含带有等待的异步调用。
let reccData = []
for (const uniformId of uniformCollectionIds) {
let watchCreatives = await CreativeCollection.findById(uniformId);
if(watchCreatives.isActive){
let recommendations = await ReccService.fetchReccSeries(watchCreatives.reccNo);
if(recommendations){
reccData.push({reccNo:recommendations.reccNo, watchList:recommendations.watchListId,...})
}
}
}
Logistics.insertMany(reccData).then(() => {
//My other business logic goes here
});
ReccService.ts
public fetchReccSeries = async (reccNo:number) => {
let getRecc = await Recc.findByNumber(reccNo)
if(getRecc){
return getRecc
}
};
上面代码的问题是,甚至在我从移动到下一次迭代中await ReccService.fetchReccSeries
得到响应之前。for..of
上面只是一个代码片段,我希望它以顺序方式执行的原因是,很多计数器/增量逻辑都基于此。
我试过的
一个.then({})
承诺确认,但它的行为仍然相同。
前置await
_for..of
for await (const uniformId of uniformCollectionIds) {
尝试Promise.All
超出了我的范围
我所期待的:
直到每次迭代中的所有执行都没有完成,下一个 for 迭代不应该调用
解决方案
推荐阅读
- scala - 使用经过身份验证的 Confluent Schema Registry 配置 Spark Structured Streaming
- r - 根据重复 ID 创建唯一 ID
- r - Shiny DownloadHandler 不下载文件
- angular - 我的代码中没有触发 Angular 生命周期更改事件
- firebase - 接收 FlutterFire 推送通知时使用 BuildContext
- python - Numba - 如何为可变数量的函数参数声明签名?
- html - 为什么我没有写在私有IP上的网站?
- boost - 无效的重载 boost::asio::io_service::run
- javascript - 是否可以在 HighChartJS 中创建分组条形图?
- yii2 - yii2如何查看自己的实体?