简单案例,然后确定:
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.id);
});
控制台为1 2 3 4 5
对我和KO来说,情况更复杂:
private todo(foot) {
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);
// How to put all dataGroup id in foot.param ?
});
return this.dashboardService.getFoo(foot);
}
代码执行以下操作:
return this.dashboardService.getFoo(foot);
在此之前:
console.log(dataGroup);
如何在重新调整(最后)之前等待订阅结束并在foot.param中添加/确定所有dataGroup id?
答案 0 :(得分:2)
todo
函数本身应该是异步的,因此它应该返回一个Observable
:
private todo(foo): Observable<any> {
const observables = [];
for (let i = 0; i < this.originalData.length; i++) {
observables.push( this.dashboardService.getDetails(this.id1[i], this.id2[i])
};
// notice that dataGroup is an array of latest value of all observables
return forkJoin(observables).map((dataGroup: any[]) => {
// do whatever you want to foo before calling the function
// remember you need to also merge if getFoo returns an observable as well
// but I assume it doesn't
return this.dashboardService.getFoo(foo);
});
}