Function mergeReadableStreams

  • Merge multiple streams into a single one, not taking order into account. If a stream ends before other ones, the other will continue adding data, and the finished one will not add any more data.

    Type Parameters

    • T

    Parameters

    • Rest...streams: ReadableStream<T>[]

      An iterable of ReadableStreams to merge.

    Returns ReadableStream<T>

    A ReadableStream that will emit the merged chunks.

    T The type of the chunks in the input/output streams.

    import { mergeReadableStreams } from "@std/streams/merge-readable-streams";
    import { assertEquals } from "@std/assert";

    const stream1 = ReadableStream.from([1, 2]);
    const stream2 = ReadableStream.from([3, 4, 5]);

    const mergedStream = mergeReadableStreams(stream1, stream2);
    const merged = await Array.fromAsync(mergedStream);
    assertEquals(merged.toSorted(), [1, 2, 3, 4, 5]);
    import { mergeReadableStreams } from "@std/streams/merge-readable-streams";
    import { assertEquals } from "@std/assert";

    const stream1 = ReadableStream.from([1, 2]);
    const stream2 = ReadableStream.from([3, 4, 5]);
    const stream3 = ReadableStream.from([6]);

    const mergedStream = mergeReadableStreams(stream1, stream2, stream3);
    const merged = await Array.fromAsync(mergedStream);
    assertEquals(merged.toSorted(), [1, 2, 3, 4, 5, 6]);