【发布时间】:2020-07-20 19:55:52
【问题描述】:
我有一个表达式,它接受一个可观察的帖子流,并为每个帖子评估一个“posterName”并合并回原始帖子。
在返回帖子之前,有没有办法调用另一个函数来评估第二个属性(当前用户是否喜欢帖子),然后将其与海报名称和原始帖子合并。
请看下面的代码和评论
this.fService.postList$.pipe(
switchMap(_posts => { // switchMap to subscribe to inner
const getUsers$ = _posts
.map(post => // array map comments into posterName fetch
this.fService.getUserName(post.uid).pipe(
// once you have the username, rx map back into the comment with the username assigned
map(userName => ({...post, posterName: userName.first_name + " " + userName.last_name})),
catchError((err) => {
// Handle specific error for this call
console.log(err)
return of({...post, posterName: "Anonymous"})
}),
first()
)
//Where can I write a second function that takes the same posts ID
//and return a promise that I can "then" so I merge the resolved
//value in to the post i.e ({...post,posterName: somename , thirdThing: somevalue})
)
return forkJoin(...getUsers$); // forkJoin them all together for parallel execution
})).subscribe(data => { //doing stuff})
【问题讨论】:
标签: angularjs promise rxjs observable