【发布时间】:2023-04-01 21:05:01
【问题描述】:
如何将 http.get.subscribe 转换为 await 以便在继续执行下一个代码之前等待它? 有问题的功能是这样的:
async ngOnInit() {
await this.setupIngredients();
console.log("this text should be displayed after FINISHED");
otherCode();
//....other code
}
我希望otherCode() 和console.log("this text should be displayed after FINISHED"); 在setupIngredients() 完成后执行。
我的 setupIngredients 函数是这样的:
setupIngredients() {
this.indicatorDataService.http.get(this.baseGetUrl + "entity=ingredients&indicator=all").subscribe(res => {
// do something with res
});
}
我仍然不知道为什么它不能转换为正确的 Promise。我尝试用 toPromise 替换 subscribe,但还是不行。
【问题讨论】:
标签: promise async-await subscribe