Reputation: 815
I'm calling service api in loop and then I want to wait for the all results. I'm not sure how can I use Observable.forkJoin here.
Component:
for(let i=0;i<data.length;i++{
this.component.getData(data[i].id).then((result: any) => {
})
}
Service:
getData(parameters:any): Promise<Object> {
return this.query(parameters)
}
Upvotes: 2
Views: 1478
Reputation: 4305
The key here is Promise.all
, which waits for all promises to resolve before executing your callback.
let promises = data.map(d => this.component.getData(d.id));
Promise.all(promises).then(results => {
console.log(results);
});
Upvotes: 1