我编写了以下代码,每次在发布请求发生之前调用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;
// }
}));
}
答案 0 :(得分:1)
当您要依次执行多个异步操作时,通常希望使用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;
})
);