在`subscribe`中等待REST API响应的Angular

huangapple go评论62阅读模式
英文:

Angular wait for rest API response in subscribe

问题

当我尝试通过客户端(Angular)获取REST API调用时,但当我订阅它时,此时函数会立即运行而不等待响应,请帮忙暂停函数。

```typescript
GetLookup(){
  let Country = this.countryService.GetAllCountry().toPromise();
}

GetLookup(){
 this.countryService.GetAllCountry().subscribe((obj){
  let Country = obj;
})
}
英文:

When I Try to get REST API Call thow client side (Angular) but when i subscribe this at that time function will runing not wait for response so please help for hold on function.

GetLookup(){
  let Country = this.countryService.GetAllCountry().toPromise();
}

OR

GetLookup(){
 this.countryService.GetAllCountry().subscribe((obj){
  let Country = obj;
})
}

答案1

得分: 1

你可以使用 async-await 的概念。

async GetLookup(){
  let Country = await this.countryService.GetAllCountry();
}
英文:

You Can Use async-await concept.

    async GetLookup(){
  let Country = await this.countryService.GetAllCountry();
}

huangapple
  • 本文由 发表于 2023年2月6日 17:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定