mergeMap

signature: mergeMap(project: function: Observable, resultSelector: function: any, concurrent: number): Observable

Map to observable, emit values.


:bulb: flatMap is an alias for mergeMap!

:bulb: If only one inner subscription should be active at a time, try
switchMap!

:bulb: If the order of emission and subscription of inner observables is
important, try concatMap!


Why use mergeMap?

This operator is best used when you wish to flatten an inner observable but want
to manually control the number of inner subscriptions.

For instance, when using switchMap each inner subscription is
completed when the source emits, allowing only one active inner subscription. In
contrast, mergeMap allows for multiple inner subscriptions to be active at a
time. Because of this, one of the most common use-case for mergeMap is
requests that should not be canceled, think writes rather than reads. Note that
if order must be maintained concatMap is a better option.

Be aware that because mergeMap maintains multiple active inner subscriptions
at once it’s possible to create a memory leak through long-lived inner
subscriptions. A basic example would be if you were mapping to an observable
with an inner timer, or a stream of dom events. In these cases, if you still
wish to utilize mergeMap you may want to take advantage of another operator to
manage the completion of the inner subscription, think
take or takeUntil. You
can also limit the number of active inner subscriptions at a time with the
concurrent parameter, seen in
example 4.

mergeMap - 图4

Examples

Example 1: mergeMap with observable

( StackBlitz |
jsBin |
jsFiddle )

  1. import { of } from 'rxjs/observable/of';
  2. import { mergeMap } from 'rxjs/operators';
  3. //emit 'Hello'
  4. const source = of('Hello');
  5. //map to inner observable and flatten
  6. const example = source.pipe(mergeMap(val => of(`${val} World!`)));
  7. //output: 'Hello World!'
  8. const subscribe = example.subscribe(val => console.log(val));
Example 2: mergeMap with promise

( StackBlitz |
jsBin |
jsFiddle )

  1. import { of } from 'rxjs/observable/of';
  2. import { mergeMap } from 'rxjs/operators';
  3. //emit 'Hello'
  4. const source = of('Hello');
  5. //mergeMap also emits result of promise
  6. const myPromise = val =>
  7. new Promise(resolve => resolve(`${val} World From Promise!`));
  8. //map to promise and emit result
  9. const example = source.pipe(mergeMap(val => myPromise(val)));
  10. //output: 'Hello World From Promise'
  11. const subscribe = example.subscribe(val => console.log(val));
Example 3: mergeMap with resultSelector

( StackBlitz |
jsBin |
jsFiddle )

  1. import { of } from 'rxjs/observable/of';
  2. import { mergeMap } from 'rxjs/operators';
  3. /*
  4. you can also supply a second argument which receives the source value and emitted
  5. value of inner observable or promise
  6. */
  7. //emit 'Hello'
  8. const source = of('Hello');
  9. //mergeMap also emits result of promise
  10. const myPromise = val =>
  11. new Promise(resolve => resolve(`${val} World From Promise!`));
  12. const example = source.pipe(
  13. mergeMap(
  14. val => myPromise(val),
  15. (valueFromSource, valueFromPromise) => {
  16. return `Source: ${valueFromSource}, Promise: ${valueFromPromise}`;
  17. }
  18. )
  19. );
  20. //output: "Source: Hello, Promise: Hello World From Promise!"
  21. const subscribe = example.subscribe(val => console.log(val));
Example 4: mergeMap with concurrent value

( StackBlitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { mergeMap, take } from 'rxjs/operators';
  3. //emit value every 1s
  4. const source = interval(1000);
  5. const example = source.pipe(
  6. mergeMap(
  7. //project
  8. val => interval(5000).pipe(take(2)),
  9. //resultSelector
  10. (oVal, iVal, oIndex, iIndex) => [oIndex, oVal, iIndex, iVal],
  11. //concurrent
  12. 2
  13. )
  14. );
  15. /*
  16. Output:
  17. [0, 0, 0, 0] <--1st inner observable
  18. [1, 1, 0, 0] <--2nd inner observable
  19. [0, 0, 1, 1] <--1st inner observable
  20. [1, 1, 1, 1] <--2nd inner observable
  21. [2, 2, 0, 0] <--3rd inner observable
  22. [3, 3, 0, 0] <--4th inner observable
  23. */
  24. const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/mergeMap.ts