我的http后端调用可能有异常,如果它失败,我的方法不会继续执行其余的指令(//做其他事情)。如何在出现异常情况下继续执行?
public updateStatus(status: string): void {
this.userApiService.user.updateStatus(status, this.user.objectKey).subscribe((response) => {
//do something else
);
}
import { finalize } from 'rxjs/operators';
public updateStatus(status: string): void
{
this.userApiService.user.updateStatus(status,this.user.objectKey)
.pipe(finalize(() =>
{
//do something even after exception
}),
.subscribe((response) =>
{
}, error => console.error(error));
}
添加catchError
算子。
import { catchError } from 'rxjs/operators';
public updateStatus(status: string): void {
this.userApiService.user.updateStatus(status,this.user.objectKey
.pipe(
catchError(e => /* handle error and return a handable error response */),
).subscribe((response) => {
//do something else
);
}