MergeMap conditionally

Viewed 115

Is there any RxJS way to conditionally call mergeMap in Angular:

return apiService.myApi.get().pipe(map(res => res), mergeMap(res => this.getPeople(res)))

to something like this:

return apiService.myApi.get().pipe(map(res => res), 
                        someCond ? mergeMap(res => this.getPeople(res)) : null)
1 Answers

No, but you can just return the original value with of() or use EMPTY to not emit anything further:

return apiService.myApi.get().pipe(
  map(res => res),
  mergeMap(res => someCond ? this.getPeople(res) : of(res)),
);
Related