gpt4 book ai didi

Angular 等待不等待

转载 作者:行者123 更新时间:2023-12-02 16:47:40 25 4
gpt4 key购买 nike

我有一些带有 await-s 设置的代码,如下所示:

async calculateDeletionResidualsData(res) {
//console.log("resss is:");
//console.log(res);

let selectedCountries = this.countriesList.filter(c => c.selected);
let selectedIndicators = this.indicatorsList.filter(i => i.selected);

await selectedCountries.forEach(async c => {
// taking each (selected) country

await selectedIndicators.forEach(async i => {
// taking each (selected) indicator

// Form the series for the calculation of residuals
let series = [];
deletionResidualsRelevantYears.forEach(y => {
series.push(res[c.label][y][i.label][0]);
});
// Calculate deletion residuals
let drr = await util.calculateDeletionResidualsStatistics(
deletionResidualsRelevantYears.length,
series,
this.deletionResidualsService
);

this.drr[c.label][i.label] = drr;
});
});

console.log("setting qrReady to true");
this.qrReady = true;
}

我可以清楚地看到对 util.calculateDeletionResidualsStatistics 的调用仍在我浏览器的网络选项卡中进行,但我还看到 console.log("setting qrReady to true"); 已经被解雇了,它不应该被解雇。为什么所有这些等待都被跳过并跳到最后一行代码?

最佳答案

await 不适用于使用回调的循环。永远不要将 await 与 forEach 一起使用。改为使用 for 循环(或任何没有回调的循环)

forEach 不是 promise 感知的。它不支持异步和等待。您不能在 forEach 中使用 await。

Learn more...

关于 Angular 等待不等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59882207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com