MergeMap

Projects each source value to an Observable which is merged in the output Observable.

Parameters

project

A function that, when applied to an item emitted by the source Observable, returns an Observable.

resultSelector

Optional. Default is undefined.

Type: number | ((outerValue: T, innerValue: ObservedValueOf, outerIndex: number, innerIndex: number) => R).

concurrent

Optional. Default is Number.POSITIVE_INFINITY.

Maximum number of input Observables being subscribed to concurrently.

Projects each source value to an Observable which is merged in the output Observable. Maps each value to an Observable, then flattens all of these inner Observables using mergeAll.

import { of, interval } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
 
const letters = of('a', 'b', 'c');
const result = letters.pipe(
  mergeMap(x => interval(1000).pipe(map(i => x+i))),
);
result.subscribe(x => console.log(x));
 
// Results in the following:
// a0
// b0
// c0
// a1
// b1
// c1
// continues to list a,b,c with respective ascending integers

The main difference between mergeMap and concatMap is that concatMap wait until the previous observable ends to start executing the next the mergeMap does not do that as you can see in the previous image.

Last updated