Skip to content

Function: zip()

zip<T>(streams): Stream<T>

Defined in: streams/zip.ts:17

Combines multiple streams by emitting an array of values, only when all streams have emitted at least one value.

After emitting, it waits for the next value from all streams.

The stream completes when any of the input streams complete. Errors from any stream propagate immediately.

Type Parameters

T

T extends readonly unknown[] = any[]

A tuple type representing the combined values from the streams.

Parameters

streams

{ [K in string | number | symbol]: Stream<T[K<K>]> | Promise<T[K<K>]> | T[K<K>][] }

An array of streams to combine.

Returns

Stream<T>

A new stream that emits an array of values.

Examples

From zip.spec.ts:4

typescript
const stream1$ = from([1, 2, 3]);
const stream2$ = from(['a', 'b', 'c']);
const stream3$ = from([true, false, true]);
const result: any[] = [];
zip([stream1$, stream2$, stream3$]).subscribe({
  next: (value: any) => result.push(value),
  complete: () => {
    expect(result).toEqual([
      [1, 'a', true],
      [2, 'b', false],
      [3, 'c', true],
    ]);
    done();
  },
  error: (err: any) => done.fail(err),
});

Released under the MIT License.