Dot Net Dev
Dot Net Dev

Reputation: 614

Iterate API response and mergeMap the subsequent requests

I am calling an API which gives a array of objects, make subsequent requests to the object and return all the observables. I have the below code.

test1() {
  let cForm = {};
  let data = [];
  let forkJoinArray = [];
  return this.http.get < any > ("http://test/c")
    .pipe(
      map(item => {
        return item
      }),
      mergeMap(data => {
        data = data.items;
        data.forEach(item => {
          const cF = this.http.get < CForm > ("http://test/cForm");
          const p = this.http.get < P > ("http://test/p");
          forkJoinArray.push(forkJoin([ of (item), cF, p]));
        });
        return forkJoinArray;
      })
    )
}

ts file

this.Service.test1().subscribe(item => {});

and in ts I want to subscribe, but now I see that I get a list of Observables. How do I iterate over the array of observables and assign the data within it to variables. Please let me know how to deal with this scenario.

Upvotes: 1

Views: 333

Answers (1)

satanTime
satanTime

Reputation: 13539

because forkJoinArray is an array of observables you need to process it, but depends on data structure you want to receive.

will emit groups of every forkJoin

return merge(...forkJoinArray);

will emit all data together

return forkJoin(...forkJoinArray);

Upvotes: 1

Related Questions