combineLatest()

Imagine you have a situation where you have set up connections with several endpoints that serve you with data. What you care about is the latest data that was emitted from each endpoint. You might be in a situation where one or several endpoints stop sending data after a while and you want to know what the last thing that happened was. In this situation, we want the ability to combine all the latest values from all of the involved endpoints. That's where the combineLatest() operator comes in. You use it in the following way:

// combination/combineLatest.js

let firstStream$ = Rx.Observable
.interval(500)
.take(3);

let secondStream$ = Rx.Observable
.interval(500)
.take(5);

let combinedStream$ = Rx.Observable.combineLatest(
firstStream$,
secondStream$
)

// emits [0, 0] [1,1] [2,2] [2,3] [2,4] [2,5]
combinedStream$.subscribe(data => console.log(data));

What we can see here is that firstStream$ stops emitting values after a while thanks to the take() operator, which limits the number of items. However, the combineLatest() operator ensures we are still given the very last value firstStream$ emitted. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.2.78