Reputation: 13
I am trying to make multiple request by using for loop and then using forkJoin to get all the data at once.
To get data, I have created one function in my service component and then inside for loop after calling that function I am subscribing it and then pushing the data into temporary variable. And after that when for loop is completed I am expecting forkJoin to subscribe and return the value.
Code:
const observables = [];
for (let i = 0; i < this.originalData.length; i++) {
this.dashboardService.getDetails(this.id1[i], this.id2[i]).subscribe(data => {
observables.push(data);
});
}
forkJoin(observables).subscribe(dataGroup => {
console.log(dataGroup);
});
If I check my network in inspect element then I can see that it is requesting those urls and the response is of 200 status but in console I don't see any error but at the same time there is no output from forkJoin subscribe. I am new to this so may be I am not understanding it correctly how to use forkJoin.
Also I am using rxjs": "^6.0.0",
Upvotes: 1
Views: 2946
Reputation: 16441
First create array of observables and then provide those observables to forkJoin
:
const observables = [];
for (let i = 0; i < this.originalData.length; i++) {
observables.push( this.dashboardService.getDetails(this.id1[i], this.id2[i])
};
forkJoin(...observables).subscribe(dataGroup => {
console.log(dataGroup);
});
Upvotes: 2