angular 同步调用api

有时候会遇到这种情况,在a函数调用完之后再调用b函数接口,这时候就要用到angular http同步请求,下面是一种案例:

 //getDetails
  public async getLocationDetails(): Promise<any>{
    
    let customBody =  {
      "appID"       :  APP_CONFIG['appID'],
      "apiVersion"  : 2,
      "eventID"     : atob(sessionStorage.getItem('lyc_event_id')),
    }

    const locationDetailsRes=await this.httpService.callEMP(environment.API.getEventInfo,customBody).toPromise()
    if(locationDetailsRes["status"] == '0'){
      /**
       * 0 is mean success. other status value is mean error
       **/
      sessionStorage.setItem('currentLocationName',locationDetailsRes['event']['eventName'])
    }else{
      this.snackBar.open(this.translateService.instant('General.Error.something_went_wrong_text'), '×', { panelClass: 'error', verticalPosition: 'top', duration: 5000 });
    }
  }
  public async onLoginFormSubmit() {
    await this.getLocationDetails();
}    

await会阻断后面函数的执行,直到当前函数执行完毕

上一篇:前端面试题


下一篇:Angular CD Part1