This is at the beginning a bit long to grasp, at least for me.
Anyways, consider this:
flatMap IS ANOTHER NAME FOR mergeMap - mergeMap method accepts an optional parameter concurrency, which defines how many Observables can be subscribed at the same time
concatMap equals mergeMap with concurrency set to 1
with mergeMap you do not lose any event emitted by the Observables you are merging as you suggested in your answer
flatMap/mergeMap - creates an Observable immediately for any source item, all previous Observables are kept alive. Note flatMap is an alias for mergeMap and flatMap will be removed in RxJS 8.
concatMap - waits for the previous Observable to complete before creating the next one
switchMap - for any source item, completes the previous Observable and immediately creates the next one
exhaustMap - source items are ignored while the previous Observable is not completed
Here is an example of how each of the operators behaves when the source is immediate items (0,1,2,3,4) and the map function creates an Observable that delays each item by 500ms:
const { mergeMap, flatMap, concatMap, switchMap, exhaustMap } = Rx.operators;
const example = operator => () =>
Rx.Observable.from([0,1,2,3,4])
.pipe(
operator(x => Rx.Observable.of(x).delay(500))
)
.subscribe(console.log, () => {}, () => console.log(`${operator.name} completed`));
const mm = example(mergeMap);
const fm = example(flatMap);
const cm = example(concatMap);
const sm = example(switchMap);
const em = example(exhaustMap);
@ZahiC, cool answer - I like the use of functional composition in the code sample. If I may, like to borrow it to illustrate a couple of extra points using timed observables.
Outer, inner, and control
These operators are all transformation operators like map(), the common feature is they have an outer and inner observable. The key difference is the way the outer observable controls the inner observable.
To contrast them, my code sample runs them in pairs, outputting values in the form [outerValue,innerValue]. I have added intervals to the test, and changed the inner delay so that there's some overlap in timing (formula used is delay((5-x)*200)).
mergeMap vs concatMap
These both output all values, the difference is the ordering.
mergeMap - Order by inner observable
[0,0],[1,0],[0,1],[2,0],[1,1],[3,0],[2,1],[4,0],[3,1],[4,1]
concatMap - Order by outer observable
[0,0],[0,1],[1,0],[1,1],[2,0],[2,1],[3,0],[3,1],[4,0],[4,1]
From the output, mergeMap outer emit can be delayed in the sequence, but concatMap follows strict outer emit sequence.
switchMap vs exhaustMap
These both throttle the output.
switchMap - Throttle by last
[3,0],[4,0],[4,1]
exhaustMap - Throttle by first
[0,0],[0,1],[4,0],[4,1]
From the output, switchMap throttles any incomplete inner emits, but exhaustMap throttles following emits until the earlier ones complete.
mergeMap vs switchMap
I threw this in because switchmap is often used in SO answers where really mergeMap should be used.
mergeMap - Order by inner observable
[0,0],[1,0],[0,1],[2,0],[1,1],[3,0],[2,1],[4,0],[3,1],[4,1]
switchMap - Throttle by last
[3,0],[4,0],[4,1]
The main takeaway is that the switchMap output is unpredictable depending on the timing of the inner observable, e.g if the inner is an http get the results can depend on connection speed.
You can choose between an interval or click for emiting outer observable values.
For the inner observable you can choose whether to emit an interval (3 items) or a http request.
Here is one more way of thinking about the difference between the different types of maps. This helped me get my head around it. I hope it might help others.
Consider the following sources:
A source producing lower case letters from the alphabet: a,b,c & d
4 separate "word" sources, each one producing 3 words starting with a particular letter from the alphabet - a,b,c or d - then completing
To illustrate the difference between different kinds of maps, we will link items from the alphabet source to their "word" sources corresponding to that letter of the alphabet, using each different map to see the different outcomes.
This transforms each input into another source, switching output to come from that new source (ie subscribing to that new source). When another alpha input arrives, the "word" source changes (we unsubscribe from the previous "word" source).
Like Concat Map except that it will ignore any inputs that come in while it is still completing the last source. The example below assumes that the alpha inputs "b" and "d" both came in while the previous mapped source was still completing, so they were ignored.
Let's say you are subscribed to a weather channel. weather announcer reads the report that passed to him after run through several operations.
If the announcer is reading one report and while reading another report comes in. If he stops reading the first report and begins to read the new report as soon as it arrives then he is doing switchMap. Because switchMap projects each source value to an observable which is merged in the output observable, emitting values only from the most "recently" projected observable.
if the radio announcer does not begin a new report until the first one is finished, we have concatMap. concatMap projects each source value to an observable which is merged in the output observable in a serialized fashion waiting for each one to complete before merging the next.
if a new report comes in while the announcer is still reading and his response is somehow to read both at the same time then we have mergeMap/flatMap. ( flatMap is an alias for mergeMap). Because mergeMap projects each source value to an observable which is merged in the output observable. "mergeMap" is a more basic version of switchMap and concatMap.