Skip to content

Function: merge()

merge<T>(...sources): Stream<T>

Defined in: streams/merge.ts:18

Merges multiple source streams into a single stream, emitting values as they arrive from any source.

This is useful for combining data from multiple independent sources into a single, unified stream of events. Unlike zip, it does not wait for a value from every stream before emitting; it emits values on a first-come, first-served basis.

The merged stream completes only after all source streams have completed. If any source stream errors, the merged stream immediately errors.

Type Parameters

T

T = any

The type of the values in the streams.

Parameters

sources

...(Stream<T> | Promise<T> | T[])[]

An array of streams to be merged.

Returns

Stream<T>

A new stream that emits values from all input streams.

Examples

From merge.spec.ts:4

typescript
const source1 = from(['source1_value1', 'source1_value2']);
const source2 = from(['source2_value1', 'source2_value2']);
const mergeStream = merge(source1, source2);
const emittedValues: any[] = [];
const subscription = mergeStream.subscribe({
  next: (value: any) => emittedValues.push(value),
  complete: () => {
    expect(emittedValues.sort()).toEqual([
      'source1_value1',
      'source1_value2',
      'source2_value1',
      'source2_value2',
    ]);

    subscription.unsubscribe();
    done();
  },
});

Released under the MIT License.