combineLatest

signature: combineLatest(observables: ...Observable, project: function): Observable

When any observable emits a value, emit the latest value from each.


:bulb: This operator can be used as either a static or instance method!

:bulb: combineAll can be used to apply combineLatest to emitted
observables when a source completes!


Why use combineLatest?

This operator is best used when you have multiple, long-lived observables that
rely on eachother for some calculation or determination. Basic examples of this
can be seen in example three,
where events from multiple buttons are being combined to produce a count of each
and an overall total, or a
calculation of BMI
from the RxJS documentation.

Be aware that combineLatest will not emit an initial value until each
observable emits at least one value
. This is the same behavior as
withLatestFrom and can be a gotcha as there will be no
output and no error but one (or more) of your inner observables is likely not
functioning as intended, or a subscription is late.

Lastly, if you are working with observables that only emit one value, or you
only require the last value of each before completion, forkJoin
is likely a better option.

combineLatest - 图3

Examples

(
example tests
)

Example 1: Combining observables emitting at 3 intervals

( StackBlitz |
jsBin |
jsFiddle )

  1. import { timer } from 'rxjs/observable/timer';
  2. import { combineLatest } from 'rxjs/observable/combineLatest';
  3. //timerOne emits first value at 1s, then once every 4s
  4. const timerOne = timer(1000, 4000);
  5. //timerTwo emits first value at 2s, then once every 4s
  6. const timerTwo = timer(2000, 4000);
  7. //timerThree emits first value at 3s, then once every 4s
  8. const timerThree = timer(3000, 4000);
  9. //when one timer emits, emit the latest values from each timer as an array
  10. const combined = combineLatest(timerOne, timerTwo, timerThree);
  11. const subscribe = combined.subscribe(
  12. ([timerValOne, timerValTwo, timerValThree]) => {
  13. /*
  14. Example:
  15. timerOne first tick: 'Timer One Latest: 1, Timer Two Latest:0, Timer Three Latest: 0
  16. timerTwo first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 0
  17. timerThree first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 1
  18. */
  19. console.log(
  20. `Timer One Latest: ${timerValOne},
  21. Timer Two Latest: ${timerValTwo},
  22. Timer Three Latest: ${timerValThree}`
  23. );
  24. }
  25. );
Example 2: combineLatest with projection function

( StackBlitz |
jsBin |
jsFiddle )

  1. import { timer } from 'rxjs/observable/timer';
  2. import { combineLatest } from 'rxjs/observable/combineLatest';
  3. //timerOne emits first value at 1s, then once every 4s
  4. const timerOne = timer(1000, 4000);
  5. //timerTwo emits first value at 2s, then once every 4s
  6. const timerTwo = timer(2000, 4000);
  7. //timerThree emits first value at 3s, then once every 4s
  8. const timerThree = timer(3000, 4000);
  9. //combineLatest also takes an optional projection function
  10. const combinedProject = combineLatest(
  11. timerOne,
  12. timerTwo,
  13. timerThree,
  14. (one, two, three) => {
  15. return `Timer One (Proj) Latest: ${one},
  16. Timer Two (Proj) Latest: ${two},
  17. Timer Three (Proj) Latest: ${three}`;
  18. }
  19. );
  20. //log values
  21. const subscribe = combinedProject.subscribe(latestValuesProject =>
  22. console.log(latestValuesProject)
  23. );
Example 3: Combining events from 2 buttons

( StackBlitz |
jsBin |
jsFiddle )

  1. import { mapTo, startWith, scan, tap, map } from 'rxjs/operators';
  2. import { fromEvent } from 'rxjs/observable/fromEvent';
  3. import { combineLatest } from 'rxjs/observable/combineLatest';
  4. // helper function to set HTML
  5. const setHtml = id => val => (document.getElementById(id).innerHTML = val);
  6. const addOneClick$ = id =>
  7. fromEvent(document.getElementById(id), 'click').pipe(
  8. // map every click to 1
  9. mapTo(1),
  10. startWith(0),
  11. // keep a running total
  12. scan((acc, curr) => acc + curr),
  13. // set HTML for appropriate element
  14. tap(setHtml(`${id}Total`))
  15. );
  16. const combineTotal$ = combineLatest(addOneClick$('red'), addOneClick$('black'))
  17. .pipe(map(([val1, val2]) => val1 + val2))
  18. .subscribe(setHtml('total'));
HTML
  1. <div>
  2. <button id='red'>Red</button>
  3. <button id='black'>Black</button>
  4. </div>
  5. <div>Red: <span id="redTotal"></span> </div>
  6. <div>Black: <span id="blackTotal"></span> </div>
  7. <div>Total: <span id="total"></span> </div>

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/combineLatest.ts