1

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?

1 Answer 1

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.