【发布时间】:2020-08-27 09:10:38
【问题描述】:
我有代码:
data.service.ts
getDataById(id: number): Observable<Model> {
return this.httpClient.get<Model>(this.api + 'data?id=' + `${id}`);
}
data.component.ts
firstFunc(id) {
this.secoundFunc(id);
console.log('res from service', this.data); // log undefined
//do something with this.data
}
secoundFunc(id) {
this.dataService.getDataById(id).subscribe(res=> {
this.data = res;
})
}
如何等待服务执行?然后继续执行剩下的代码。 我知道http请求是作为异步操作完成的。我用过,promises,async/await,setTimeout,但这些都没有帮助。
【问题讨论】:
-
我用过,promises,async/await,setTimeout....可以在帖子里分享一下吗?
标签: angular http asynchronous promise async-await