有時(shí)候會(huì)遇到這種情況,在a函數(shù)調(diào)用完之后再調(diào)用b函數(shù)接口,這時(shí)候就要用到angular http同步請(qǐng)求,下面是一種案例:
//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會(huì)阻斷后面函數(shù)的執(zhí)行,直到當(dāng)前函數(shù)執(zhí)行完畢
本文摘自 :https://www.cnblogs.com/