Skip to content

Function: combineLatest()

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

Defined in: streams/combineLatest.ts:17

Combines multiple streams and emits a tuple containing the latest values from each stream whenever any of the source streams emits a new value.

This operator is useful for scenarios where you need to react to changes in multiple independent data sources simultaneously. The output stream will not emit a value until all source streams have emitted at least one value. The output stream completes when all source streams have completed.

Type Parameters

T

T extends 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 a tuple of the latest values from all source streams.

Examples

From combineLatest.spec.ts:4

typescript
const firstTimer = timer(0, 50);
const secondTimer = timer(25, 50);
const combinedTimers = combineLatest([firstTimer, secondTimer]);
const expectedValues = [
  [0, 0],
  [1, 0],
  [1, 1],
  [2, 1],
  [2, 2],
];
let index = 0;
let subscription = combinedTimers.subscribe({
  next: (latestValues: any) => {
    try {
      expect(latestValues).toEqual(expectedValues[index]);
      index++;

      if (index === expectedValues.length) {
        subscription.unsubscribe();
        done();
      }
    } catch (error) {
      subscription.unsubscribe();
      fail(error);
    }
  },
  error: (error) => {
    subscription.unsubscribe();
    fail(error);
  },
});

Released under the MIT License.