0

I am using concat to call multiple API sequentially, but when error, concat stop call api, so how to get error and continue to call next API, here is my code:

getData() {
    concat(...this.data.map(asset =>
      this.assetService.requestData({ assetId: asset._id })
    ))
      .pipe(catchError(error => {
        console.log(error);
        // return of(null);
        this.error = error;
        return throwError(error);
      }))
      .subscribe();

  }
2
  • you can try to add .retry(N) before pipe() function is called Commented Dec 9, 2019 at 9:51
  • @akkonrad but I want after 5 times trying if it still get error, I can continue to get another api Commented Dec 9, 2019 at 9:54

1 Answer 1

1

Error catching should happen individually for each observable. You can simply return an empty observable:

import { EMPTY } from 'rxjs';

concat(...this.data.map(asset =>
  this.assetService.requestData({assetId: asset._id})
    .pipe(catchError(error => {
      console.log(error);
      this.error = error;
      return EMPTY;
    }))
  ))
  .pipe(filter(data => !!data)) // add this line if you do not want to see nulls in result
  .subscribe();


Another option would be using onErrorResumeNext function:

import { onErrorResumeNext } from 'rxjs';

onErrorResumeNext(...this.data.map(asset => 
  this.assetService.requestData({assetId: asset._id})
)
.subscribe();
Sign up to request clarification or add additional context in comments.

1 Comment

the second way works with me but if how can get Error when using onErrorResumeNext? I tried to add pipe but it didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.