[英]How to send Multiple Http request sequentially in Angular
我寫了下面的代碼,每次在發布請求發生之前調用一個API,第一個API被調用而第二個API沒有被調用
public post(postUrl: string, model: any): Observable<any> {
return this.validateTokenStatus().pipe(map(response => {
console.log('response', response);
// if (response) {
console.log('response2', response);
const url = `${environment.webApiUrl}/${postUrl}`;
this.spinnerService.start();
console.log('response21', response);
return this._http.post(url, model).pipe(map((res: any) => {
console.log('response11', response);
this.spinnerService.stop();
return res;
},
error => {
console.log('error');
return error;
}));
// } else {
// console.log('response3', response);
// return true;
// }
}));
}
當您要依次執行多個異步操作時,通常希望使用mergeMap,switchMap或concatMap中的一個。 在這種情況下,可能會發生以下情況:
return this.validateTokenStatus()
.pipe(
switchMap(response => {
const url = `${environment.webApiUrl}/${postUrl}`;
this.spinnerService.start();
return this._http.post(url, model);
}),
map((res: any) => {
this.spinnerService.stop();
return res;
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.