0
我正在使用observables,我正在努力瞭解如何在循環中使用它們。需要幫助瞭解鏈式觀察值
在我的項目中,我們加載了一個部件列表,然後循環遍歷每個部件,並且對於循環的每個迭代,我們需要請求一個資源,在應用該資源之前需要等待服務器進行響應。一旦完成,它應該轉移到下一次迭代,最後一次循環的所有迭代都完成後,可觀測值應該完整並且允許應用程序繼續。然而,在繼續前進之前,應用程序並未等待循環的所有迭代完成。
下面是我正在考慮的代碼的示例(請注意這是Angular 2)。
public load(): void {
this.loadParts(this.parts).subscribe(() => {
// This code should wait to run until all of these observables are complete. However for some reason I get here before I get to the code inside the httpService.get observable inside the loadMaterial method below.
}
}
private loadParts(parts: Part[]): Observable<any> {
Observable.create((observer: any) => {
for(part of parts) {
this.applyMaterial(part.material).subscribe();
}
// This observer should be completed when the for loop is done and all materials have been applied.
observer.next();
observer.complete();
});
}
private applyMaterial(material: Material[]): Observable<any> {
this.loadMaterial(material.id).subscribe(() => {
// We need to request the material from the server and wait for the response before applying it here.
}
}
private loadMaterial(materialId: String): Observable<any> {
this.httpService.get(API.LoadMaterialsURL + '/' + materialId).subscribe((response: any) => {
// Update the material service to include the material data returned by the response.
});
}