【发布时间】:2021-11-18 23:54:25
【问题描述】:
我想了解如何在我的情况下使用 await。
我有这个代码:
updateMap() {
this.paramsTemp = 0;
if(this.updateMapCheck == true){
this.loading = true;
this.arrOfDisplays.forEach((display, index) => {
if (display.removed == true) {
if (this.locationsToSend[index + 1]) {
this.drawOneTrip(index, index + 1, index); // here after first one finish we go to another call, here i need await?
display.removed = false;
}
}
});
// after finish above i want to go to this.markerArr...
this.markersArr.forEach((marker, index) => {
marker.set("label", { text: "" + index, color: "white" });
});
// here most important, if above finish i want to call this.changeTime()
// wait to finish every think above to call changeTime()
this.changeTime();
// while every think finish in changeTime() i want to do last 2 line.
this.loading = false;
this.map.setZoom(14);
}
else{
this.showToasterErrorUpdateMap();
}
}
我在代码中输入了所有需要的信息。
上述情况如何使用await?
在我为每个步骤使用 setTimeout 之前大约有时间,但不能完美地工作,因为编译器可能会在完成第一步之前转到另一个步骤。
【问题讨论】:
-
这段代码的哪些部分是异步的?目前还不清楚您要等待什么。
-
简而言之,除非您需要,否则您不应该这样做,因为第三方库使用了 Promise。异步操作应该可以通过 angular imo 中的 observables 和替换来解决
-
@Bergi 正如我们在代码中看到的那样,我在注释(//)之间有 4 个第 4 部分作业,每个部分都应该在执行第二部分之前完成。
-
@Kayo 当然,但如果所有功能都同步,它们确实按顺序完成。哪些函数调用是异步的?
-
我需要知道的,它需要是异步方法吗?了解我到底想要什么:在 shell 脚本中的每一行中,当行完成执行编译器时,编译器转到另一行,我的意思是,我想确保该方法的每个部分在执行另一部分之前完成执行,如果你看到 this.loading = false ,这部分意味着当每件事完成时停止加载器。 @Bergi
标签: javascript angular async-await