如何通过早期返回摆脱多个嵌套的switchMap
我得到了 3 个返回即将发生的、当前的、过去的事件的端点。我应该只展示未来最远的那个。为了优化调用而不是一次调用所有端点。我编写了一个简单的 RxJs 流,在其中调用第一个端点,如果它不返回数据,则调用第二个,依此类推。代码如下所示:
this.eventsService.getUpcoming(id).pipe(
switchMap((upcoming) => {
if (upcoming.length) {
return of(upcoming);
}
return this.eventsService.getCurrent(id).pipe(
switchMap((current) => {
if (current.length) {
return of(current);
}
return this.eventsService.getPast(id)
})
);
}),
// some other pipe operators map etc.
开关映射中可能没有嵌套的开关映射吗?