[英]Angular 8 return observable after chain of async calls
我有一個簡單的函數,它一個接一個地調用 3 個異步函數,如下所示:
getData() {
this.getUsers('2')
.pipe(
flatMap((data: any) => {
return this.getPosts(data.id);
}),
flatMap((data: any) => {
return this.getPosts(data[0].userId);
})
).subscribe(results => {
return new Observable((observer) => {
observer.next(results);
observer.complete();
});
});
}
我希望能夠以某種方式調用: this.getdata.subscribe()
並且在所有 3 個 flatMap 完成時調用它。 我假設我必須讓 getData() 函數返回一個我試圖在底部做的可觀察對象,但它不起作用,我無法在 getData() 函數上調用 .subscribe 。
像這樣嘗試:
getData() {
return this.getUsers('2')
.pipe(
flatMap((data: any) => {
return this.getPosts(data.id);
}),
flatMap((data: any) => {
return this.getPosts(data[0].userId);
})
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.