Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - RxJs ForkJoin How To Continue Multiple Requests Even After A Error

I am querying a single API endpoint multiple times except with different parameters. For what ever reason some of these requests may fail and return a 500 error. If they do i still want the other requests to carry on and return me the data of all the successfull requests.

let terms = [];
terms.push(this.category.category);
terms = terms.concat(this.category.interests.map((x) => x.category));

for (let i = 0; i < terms.length; i++) {

    const params = {
        term: terms[i],
        mode: 'ByInterest'
    };


    const request = this.evidenceService.get(this.job.job_id, params).map((res) => res.interactions);

    this.requests.push(request);

}

const combined = Observable.forkJoin(this.requests);

combined.subscribe((res) => {
    this.interactions = res;
});
like image 275
Kay Avatar asked May 02 '18 08:05

Kay


People also ask

What will happen if error happens in forkJoin?

Overall, in order for forkJoin to emit a value, all given observables have to emit something at least once and complete. If any given observable errors at some point, forkJoin will error as well and immediately unsubscribe from the other observables.

Does forkJoin maintain order?

It's worth noting that the forkJoin operator will preserve the order of inner observables regardless of when they complete.

Is forkJoin sequential?

In parallel computing, the fork–join model is a way of setting up and executing parallel programs, such that execution branches off in parallel at designated points in the program, to "join" (merge) at a subsequent point and resume sequential execution.

Is forkJoin deprecated?

forkJoin Improvements This is one of my favorites. The forkJoin observable now takes a dictionary of sources: Moreover, there is one deprecation — forkJoin(a, b, c, d) should no longer be used; Instead, pass an array such as forkJoin([a, b, c, d]) .


1 Answers

You could use rxjs catchError :

const request = this.evidenceService.get(this.job.job_id, params)
.pipe(map((res) => res.interactions),
catchError(error => of(undefined)));
like image 190
ibenjelloun Avatar answered Nov 04 '22 04:11

ibenjelloun