简体   繁体   中英

Angular how continue method execution even if the http calls got error

My http backend call may have exceptions, if it fails, my method doesn't continue with the rest of the instructions (//do something else). How to continue the execution even if there are exceptions?

  public updateStatus(status: string): void {
    this.userApiService.user.updateStatus(status, this.user.objectKey).subscribe((response) => {

      //do something else

    );
  }

Add the catchError operator.

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
    );
  }
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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM