RXjs:将 2 个开关映射合并为 1 个?

RXjs: combine 2 switchmaps into 1?

上下文

原样

使用下面的代码,我可以处理上面的上下文。

 this.http$.get<IWishResponse[]>(environment.apiUrl + 'wish/' + wishID)
 .pipe(
    map(wishResponse => wishResponse[0]),
    switchMap(wishResponse => this.peopleService.getPersonById(wishResponse.receiver).pipe(
        switchMap(personResponse => this.convertWishResponse(wishResponse, personResponse) )
    )),
 )          

问题

虽然我有一个工作代码,但感觉对于这种问题来说代码太多了。我想做什么:

将所有这些逻辑合并到一个 switchmap 中而不是嵌套 switchMap?感觉现在冗余代码很多...

最重要的是,我想通过改进上面的代码来更多地了解 RXjs。 谢谢你帮助我...

您可以通过 pluck 将链条展平并将第一个 api 结果与 map 一起传递,并且因为您正在发出两个依赖异步请求,所以您不能真正将两个 switchMap 组合成一.

 this.http$.get<IWishResponse[]>(environment.apiUrl + 'wish/' + wishID)
 .pipe(
    map(wishResponse => wishResponse[0]),
    switchMap(wishResponse => this.peopleService.getPersonById(wishResponse.receiver).pipe(map(person=>[person,wishResponse])),
    switchMap(([personResponse,wishResponse]) => this.convertWishResponse(wishResponse, personResponse) ))