Angular 4、多次api调用并等待结果
Angular 4, multiple api calls and wait for results
我正在循环调用服务 api,然后我想等待所有结果。我不确定如何在这里使用 Observable.forkJoin。
组件:
for(let i=0;i<data.length;i++{
this.component.getData(data[i].id).then((result: any) => {
})
}
服务:
getData(parameters:any): Promise<Object> {
return this.query(parameters)
}
这里的关键是 Promise.all
,它会在执行回调之前等待所有承诺解决。
let promises = data.map(d => this.component.getData(d.id));
Promise.all(promises).then(results => {
console.log(results);
});
我正在循环调用服务 api,然后我想等待所有结果。我不确定如何在这里使用 Observable.forkJoin。
组件:
for(let i=0;i<data.length;i++{
this.component.getData(data[i].id).then((result: any) => {
})
}
服务:
getData(parameters:any): Promise<Object> {
return this.query(parameters)
}
这里的关键是 Promise.all
,它会在执行回调之前等待所有承诺解决。
let promises = data.map(d => this.component.getData(d.id));
Promise.all(promises).then(results => {
console.log(results);
});