🌊 Interleaves streams, forwarding every chunk as soon as any source produces it.

Warning

Because the sources are read concurrently, chunk order between them is not defined — only the order within a single source is. Use concatStreams when order across sources matters.

Syntax

TypeScript
import { mergeStreams } from '@opentf/std';

mergeStreams<T>(...streams: ReadableStream<T>[]): ReadableStream<T>;

Parameters

NameTypeDescription
streamsReadableStream<T>[]The streams to merge.

Returns

A stream over every chunk, as it arrives. It closes once every source has closed, and errors as soon as any source errors.

Examples

TypeScript
const events = mergeStreams(clicks, keys);
await streamToArray(events)

A slow source does not hold up a fast one:

TypeScript
// `fast` arrives first even though `slow` was listed first.
await streamToArray(mergeStreams(slow, fast)) //=> ['fast', 'slow']

Order within each source is still guaranteed:

TypeScript
const merged = await streamToArray(mergeStreams(a, b));
merged.filter((c) => c.startsWith('a')) //=> ['a1', 'a2', 'a3']
Last updated on
Edit this page