Skip to content

@epikodelabs/streamix


@epikodelabs/streamix / mergeMap

Function: mergeMap()

mergeMap<T, R>(project, concurrent?): Operator<T, R>

Defined in: projects/libraries/streamix/src/lib/operators/mergeMap.ts:40

Creates a stream operator that maps each value from the source stream to an "inner" stream and merges all inner streams concurrently into a single output stream.

For each value from the source stream:

  1. The project function is called with the value and its index.
  2. The returned value is normalized into a stream using fromAny.
  3. The inner stream is consumed concurrently with all other active inner streams.
  4. Emitted values from all inner streams are interleaved into the output stream.

This operator is useful for performing parallel asynchronous operations while preserving all emitted values in a merged output with correct temporal ordering.

Type Parameters

T

T = any

The type of values in the source stream.

R

R = any

The type of values emitted by the inner and output streams.

Parameters

project

(value, index) => Stream<R> | MaybePromise<R> | R[]

A function that maps a source value and its index to either:

concurrent?

number = Infinity

Maximum number of concurrent inner streams (default: Infinity).

Returns

Operator<T, R>

An Operator instance that can be used in a stream's pipe method.

Example

typescript
// Process HTTP requests with max 3 concurrent
stream(urls).pipe(
  mergeMap(url => fetch(url), 3)
)

Released under the MIT License.