BACK
ReferencemergeMap
Reference · 22

mergeMap

Runs inner streams in parallel.

Signature

mergeMap(project, concurrent?: number): OperatorFunction

Marble diagram

outer:    --a---b---|
inner(a): --1-2-3|
inner(b):     --1-2-3|
result:   --1-2-1-3-2-3-|

What it does

For each value it starts a new inner Observable, cancelling nothing. All active inner streams run in parallel (no limit by default). The concurrent parameter caps the number of simultaneous subscriptions — extras wait in a queue.

When to use

Parallel HTTP requests (loading a list of thumbnails, for example), handling events where order and cancellation don't matter. mergeMap(fn, 3) is the standard download-pool pattern.

Gotcha

Without concurrent, a stream of clicks gives you as many simultaneous requests as clicks. For critical operations, exhaustMap is preferable.

See also

switchMap, concatMap, forkJoin

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...