Observable on error call a second observable

42 views Asked by At

I want to call a second observable when the first observable get error 400.

this.service.runTest(resource)
  .subscribe(
    response => console.log(response),
    err => {
      this.service.runTest2(resource)
        .subscribe(
          response => {
            setTimeout(()=>{this.resultDisplay = response}, 4000);

          },
          err => console.log(err)
        )
    }
  );
1

There are 1 answers

3
bryan60 On

Try using the catch operator to catch the error and react accordingly:

this.service.runTest(resource)
  .catch(err => 
      (err.status === 400) 
          ? this.service.runTest2(resource).delay(4000)
          : Observable.throw(err))
  .subscribe(
    response => console.log(response),
    err => err => console.log(err));