Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjs: Observable.combineLatest vs Observable.forkJoin

Tags:

rxjs

rxjs5

I'm wondering what are the differences between Observable.combineLatest and Observable.forkJoin?

As far as I can see, the only difference is forkJoin expects the Observables to be completed, while combineLatest returns the latest values.

like image 457
Tuong Le Avatar asked Jan 22 '17 23:01

Tuong Le


People also ask

What is the difference between combineLatest and forkJoin?

forkJoin - When all observables complete, emit the last emitted value from each. combineLatest - When any observable emits a value, emit the latest value from each.

What is observable combineLatest?

combineLatest allows to merge several streams by taking the most recent value from each input observable and emitting those values to the observer as a combined output (usually as an array).

Is forkJoin deprecated?

forkJoin Improvements Moreover, there is one deprecation — forkJoin(a, b, c, d) should no longer be used; Instead, pass an array such as forkJoin([a, b, c, d]) .

What is the difference between forkJoin and zip?

forkJoin waits for all argument Observables to complete and then emit an array of last emitted values (to compare: zip emits an array of values with same emission index).


1 Answers

Not only does forkJoin require all input observables to be completed, but it also returns an observable that produces a single value that is an array of the last values produced by the input observables. In other words, it waits until the last input observable completes, and then produces a single value and completes.

In contrast, combineLatest returns an Observable that produces a new value every time the input observables do, once all input observables have produced at least one value. This means it could have infinite values and may not complete. It also means that the input observables don't have to complete before producing a value.

like image 174
GregL Avatar answered Sep 21 '22 17:09

GregL