Reputation: 1511
I want to chain multiple request to the urls which I get from the array. Before next chain i want to wait for the previous one to finish. It's does not matter if the previous fails or not. I was trying to this with forkJoin but I know that if one of the request fails it will return error.
this.dataServers.forEach(dataServer => {
observableBatch.push(this.getFoodsByQuery(dataServer.url, query));
});
return Observable.forkJoin(observableBatch).subscribe(data => {
this.searchingServer.next(null);
observer.complete();
});
I do not know how to do this with flatMap.
Upvotes: 3
Views: 5682
Reputation: 1827
In your case suitable operator is concat
Concatenates multiple Observables together by sequentially emitting their values, one Observable after the other.
Example:
// Batch of requests
let batch = [
Observable.of(1),
Observable.of(2).delay(1500),
Observable.throw('Error'),
Observable.of(3).delay(300),
Observable.throw('Error again'),
Observable.of(4).delay(600)
];
// Catch (ignore) error of every request
batch = batch.map(obs => {
return obs.catch(err => Observable.of(err));
});
// Concat observables
Observable.concat(...batch).subscribe({
next: v => console.log(v),
complete: () => console.log('Complete')
});
Output:
1
2
'Error'
3
'Error again'
4
'Complete'
Upvotes: 8
Reputation: 514
Have you tried using OnErrorResumeNext() with forkJoin?
return Observable.forkJoin(observableBatch)
.OnErrorResumeNext()
.subscribe(data => {
this.searchingServer.next(null);
observer.complete();
});
Upvotes: 0