Как я могу преобразовать http.get.subscribe в await, чтобы я мог дождаться его, прежде чем перейти к следующему коду? Проблема c заключается в следующем:
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
});
}
Я до сих пор не знаю, почему это не приведет к правильному обещанию. Я попытался заменить подписку на toPromise, но все равно это не работает.