Reputation: 332
I would like to make several HTTP request asynchronously, then combine all of the response together into an array.
Something like below:
getSamples(genes) {
genes.forEach(gene => {
//This is the HTTP get request from other service which returns an observable array
this.vsal.getSamples(gene).subscribe(sampleRequest => {
//I want to join all the responses into sampleIdsSource
this.sampleIdsSource.next(sampleRequest.samples);
},
e => {
this.error.next(e);
})
});
}
What's the best way to do this?
Upvotes: 1
Views: 66
Reputation: 96891
Just turn each gene
into an Observable and then forkJoin
them. forkJoin
will wait until all source Observable complete and will emit a single array with their results.
getSamples(genes) {
const observables = genes.map(gene => this.vsal.getSamples(gene));
return forkJoin(observable);
}
Upvotes: 5