Remove secondary mappings for flattening operators
See original GitHub issueFor operators like mergeMap
, switchMap
, concatMap
, etc, we have a secondary selector. It’s not used a lot in practice that I’ve seen. Originally, it added a lot of value in terms of perf because it prevented a closure… however, in practice it makes it harder to isolate errors from observation chains, and in a world with Turbofan, I’m not sure the performance benefit is there. We could eliminate a good amount of code in the library written to support this.
Use and Alternatives
Observable.of(1, 2, 3).mergeMap(a => range(0, a), (a, b) => a + b);
/// which is mostly the same as
Observable.of(1, 2, 3).mergeMap(a => range(0, a).map(b => a + b))
// however if there's an error in that second map, you're forced to kill the whole observable...
Observable.of(1, 2, 3)
.mergeMap(a => range(0, a), (a, b) => { throw new Error('lol') })
.catch(err => empty())
// rather than this, which is much more common
Observable.of(1, 2, 3)
.mergeMap(
a => range(0, a)
.map(b => a + b)
.catch(err => empty())
)
Proposed Change
Eliminate the second argument to mergeMap
, concatMap
, switchMap
, et. al. and instruct people to use .map
within the projection function if they want a secondary level of mapping.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:16 (12 by maintainers)
Top Results From Across the Web
Flattening map operators in RxJS
In RxJS We have four flattening map operators: ... acts the same as concatMap and mergeMap in the context of flattening and mapping....
Read more >Learn RxJS switchMap, mergeMap, concatMap and ... - Medium
There are 4 “MAPPING + FLATTENING” Operators. let's call them “mappening” operators from now on —. (1) mergeMap() , (2) switchMap() ...
Read more >RxJs Mapping: switchMap vs mergeMap vs concatMap vs ...
Learn in-depth the merge, switch, concat and exhaust strategies and their operators: concatMap, mergeMap, switchMap and exhaustMap.
Read more >Flattening Operators in RxJS 101 - JavaScript Marathon
Learn about mergeMap, concatMap, switchMap, exhaustMap, and which ones to use when with Ben Lesh, author of RxJSIf you enjoyed these ...
Read more >Using Data Flow Operators - Oracle Help Center
In Data Integration, data flow operators represent input sources, output targets, and transformations that can be used in a data flow.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
With regards to that argument I made above, the problem isn’t so much with the “throwable” part as it is in patterns like you’d see in redux-observable or ngrx, where you might have something that throws, moving the catch outside of the flattening operation will kill your entire stream, but catching it inside gets weird with that secondary map:
As you can see it gets ugly fast… But by just using a
map
it cleans it up.I’ve not a lot to technically add to this discussion. I can only say that I very often use the secondary selector, and my team has followed my practice. This change would cause us to have to touch a lot of files across quite a number of projects. However according to your observations, we’re exceptions to the rule …