【发布时间】:2019-04-16 10:53:02
【问题描述】:
我有一个要完成的操作列表,如果设置了布尔值 promptRequired,我会显示一个对话框并根据它的值执行一些操作。
这里的问题是for循环并行运行并且所有动作同时执行,但是我希望for循环同步运行并且循环必须等到对话框关闭。有解决办法吗?
async runActions() {
for (const action of Actions) {
if(action.promptRequired) {
const dialogRef = this.promptDialog.open(PromptDialogComponent, {
data: {action: action,
},
});
// await dialogRef.afterClosed();
}
}
const status = await this.httpService.getRequest('runAction', action)
// Do Some Action based on status
}
}
}
【问题讨论】:
-
再次,read the documentation。你甚至自己写了答案……
-
等待对话框关闭不起作用,下一次迭代不会等到对话框关闭
-
那么请提供一个minimal reproducible example 重现该问题。
-
问题不在于对话框,而在于您的代码。我真的建议你阅读 the documentation of await 和 the documentation of the dialog :一个处理 promise,另一个处理 observables。
标签: angular asynchronous async-await angular-material angular6