[英]Angular 8 return observable after chain of async calls
I have a simple function that calls 3 async functions one after another like this:我有一个简单的函数,它一个接一个地调用 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();
});
});
}
I would like someway to be able to call something like: this.getdata.subscribe()
and that be called when all 3 flatMaps are completed.我希望能够以某种方式调用:
this.getdata.subscribe()
并且在所有 3 个 flatMap 完成时调用它。 I assume I would have to have the getData() function return an observable which I tried to do at the bottom but it does not work and I am not able to call .subscribe on the getData() function.我假设我必须让 getData() 函数返回一个我试图在底部做的可观察对象,但它不起作用,我无法在 getData() 函数上调用 .subscribe 。
Try like this:像这样尝试:
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.