combineLatest

function stable

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

combineLatest<O extends ObservableInput<any>, R>(...args: any[]): Observable<R> | Observable<ObservedValueOf<O>[]>

Parameters

args

Type: any[].

Returns

Observable<R> | Observable<ObservedValueOf<O>[]>: An Observable of projected values from the most recent values from each input Observable, or an array of the most recent values from each input Observable.

Description

Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula.

combineLatest marble diagram

combineLatest combines the values from all the Observables passed in the observables array. This is done by subscribing to each Observable in order and, whenever any Observable emits, collecting an array of the most recent values from each Observable. So if you pass n Observables to this operator, the returned Observable will always emit an array of n values, in an order corresponding to the order of the passed Observables (the value from the first Observable will be at index 0 of the array and so on).

Static version of combineLatest accepts an array of Observables. Note that an array of Observables is a good choice, if you don't know beforehand how many Observables you will combine. Passing an empty array will result in an Observable that completes immediately.

To ensure the output array always has the same length, combineLatest will actually wait for all input Observables to emit at least once, before it starts emitting results. This means if some Observable emits values before other Observables started emitting, all these values but the last will be lost. On the other hand, if some Observable does not emit a value but completes, resulting Observable will complete at the same moment without emitting anything, since it will now be impossible to include a value from the completed Observable in the resulting array. Also, if some input Observable does not emit any value and never completes, combineLatest will also never emit and never complete, since, again, it will wait for all streams to emit some value.

If at least one Observable was passed to combineLatest and all passed Observables emitted something, the resulting Observable will complete when all combined streams complete. So even if some Observable completes, the result of combineLatest will still emit values when other Observables do. In case of a completed Observable, its value from now on will always be the last emitted value. On the other hand, if any Observable errors, combineLatest will error immediately as well, and all other Observables will be unsubscribed.

Examples

Combine two timer Observables

import { combineLatest, timer } from 'rxjs';

const firstTimer = timer(0, 1000); // emit 0, 1, 2... after every second, starting from now
const secondTimer = timer(500, 1000); // emit 0, 1, 2... after every second, starting 0,5s from now
const combinedTimers = combineLatest([firstTimer, secondTimer]);
combinedTimers.subscribe(value => console.log(value));
// Logs
// [0, 0] after 0.5s
// [1, 0] after 1s
// [1, 1] after 1.5s
// [2, 1] after 2s

Combine a dictionary of Observables

import { combineLatest, of } from 'rxjs';
import { delay, startWith } from 'rxjs/operators';

const observables = {
  a: of(1).pipe(delay(1000), startWith(0)),
  b: of(5).pipe(delay(5000), startWith(0)),
  c: of(10).pipe(delay(10000), startWith(0))
};
const combined = combineLatest(observables);
combined.subscribe(value => console.log(value));
// Logs
// {a: 0, b: 0, c: 0} immediately
// {a: 1, b: 0, c: 0} after 1s
// {a: 1, b: 5, c: 0} after 5s
// {a: 1, b: 5, c: 10} after 10s

Combine an array of Observables

import { combineLatest, of } from 'rxjs';
import { delay, startWith } from 'rxjs/operators';

const observables = [1, 5, 10].map(
  n => of(n).pipe(
    delay(n * 1000),   // emit 0 and then emit n after n seconds
    startWith(0),
  )
);
const combined = combineLatest(observables);
combined.subscribe(value => console.log(value));
// Logs
// [0, 0, 0] immediately
// [1, 0, 0] after 1s
// [1, 5, 0] after 5s
// [1, 5, 10] after 10s

Use map operator to dynamically calculate the Body-Mass Index

import { combineLatest, of } from 'rxjs';
import { map } from 'rxjs/operators';

const weight = of(70, 72, 76, 79, 75);
const height = of(1.76, 1.77, 1.78);
const bmi = combineLatest([weight, height]).pipe(
  map(([w, h]) => w / (h * h)),
);
bmi.subscribe(x => console.log('BMI is ' + x));

// With output to console:
// BMI is 24.212293388429753
// BMI is 23.93948099205209
// BMI is 23.671253629592222

Overloads

combineLatest(arg: T): Observable<unknown>

You have passed any here, we can't figure out if it is an array or an object, so you're getting unknown. Use better types.

Parameters

arg

Something typed as any

Returns

Observable<unknown>

combineLatest(sources: []): Observable<never>

Parameters

sources

Type: [].

Returns

Observable<never>

combineLatest(sources: readonly any[]): Observable<A>

Parameters

sources

Type: readonly any[].

Returns

Observable<A>

combineLatest(sources: readonly any[], resultSelector: (...values: A) => R, scheduler: SchedulerLike): Observable<R>

Deprecation Notes

The scheduler parameter will be removed in v8. Use scheduled and combineLatestAll. Details: https://rxjs.dev/deprecations/scheduler-argument

Parameters

sources

Type: readonly any[].

resultSelector

Type: (...values: A) => R.

scheduler

Type: SchedulerLike.

Returns

Observable<R>

combineLatest(sources: readonly any[], resultSelector: (...values: A) => R): Observable<R>

Parameters

sources

Type: readonly any[].

resultSelector

Type: (...values: A) => R.

Returns

Observable<R>

combineLatest(sources: readonly any[], scheduler: SchedulerLike): Observable<A>

Deprecation Notes

The scheduler parameter will be removed in v8. Use scheduled and combineLatestAll. Details: https://rxjs.dev/deprecations/scheduler-argument

Parameters

sources

Type: readonly any[].

scheduler

Type: SchedulerLike.

Returns

Observable<A>

combineLatest(...sources: any[]): Observable<A>

Deprecation Notes

Pass an array of sources instead. The rest-parameters signature will be removed in v8. Details: https://rxjs.dev/deprecations/array-argument

Parameters

sources

Type: any[].

Returns

Observable<A>

combineLatest(...sourcesAndResultSelectorAndScheduler: [any, (...values: A) => R, SchedulerLike]): Observable<R>

Deprecation Notes

The scheduler parameter will be removed in v8. Use scheduled and combineLatestAll. Details: https://rxjs.dev/deprecations/scheduler-argument

Parameters

sourcesAndResultSelectorAndScheduler

Type: [any, (...values: A) => R, SchedulerLike].

Returns

Observable<R>

combineLatest(...sourcesAndResultSelector: [any, (...values: A) => R]): Observable<R>

Deprecation Notes

Pass an array of sources instead. The rest-parameters signature will be removed in v8. Details: https://rxjs.dev/deprecations/array-argument

Parameters

sourcesAndResultSelector

Type: [any, (...values: A) => R].

Returns

Observable<R>

combineLatest(...sourcesAndScheduler: [any, SchedulerLike]): Observable<A>

Deprecation Notes

The scheduler parameter will be removed in v8. Use scheduled and combineLatestAll. Details: https://rxjs.dev/deprecations/scheduler-argument

Parameters

sourcesAndScheduler

Type: [any, SchedulerLike].

Returns

Observable<A>

combineLatest(sourcesObject: { [x: string]: never; }): Observable<never>

Parameters

sourcesObject

Type: { [x: string]: never; }.

Returns

Observable<never>

combineLatest(sourcesObject: T): Observable<{ [K in keyof T]: ObservedValueOf<T[K]>; }>

Parameters

sourcesObject

Type: T.

Returns

Observable<{ [K in keyof T]: ObservedValueOf<T[K]>; }>

See Also

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/combineLatest