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;
});
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.
It's worth noting that the forkJoin operator will preserve the order of inner observables regardless of when they complete.
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.
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]) .
You could use rxjs catchError :
const request = this.evidenceService.get(this.job.job_id, params)
.pipe(map((res) => res.interactions),
catchError(error => of(undefined)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With