HomeOur TeamContact
Angular
Combination operators in RxJS
Nisar
Nisar
May 20, 2021
1 min

When working with data streams, we might want to combine multiple data streams into one stream to be able to get one result in some scenarios.

Majorly used combination operators are

  • combineLatest
  • forkJoin
  • withLatestFrom

Three scenario when we want to use combination operators can be:

  1. Joining data from multiple streams to one stream
  2. Merge together all the values to create one stream
  3. Combine data from different streams and emit a stream of combined values

combineLatest

The combineLatest operator is one of the combination operators that emits the last value from each of the observable streams when the observable emits a value.

You won’t see the result until each of the streams have emitted at least one value.

What happens if one of the observable didn’t complete?

Well, the output observable won’t complete too

forkJoin

When the observable emits only one value or you want only the last value from each stream, that is when forkJoin proves to be of help. forkJoin allows creation of the output stream with the last values from all the input streams.

When we are dealing with multiple API calls, and do not want to render the view until all the http requests have completed, forkJoin is the right thing to do.

However, you might end up in a trouble if you choose to use forkJoin when one of the input streams might not complete.

withLatestFrom

This is a pipe-able operator which creates an output stream with the latest values from the other input streams only once each of the streams have emitted at least one value.

Now I was confused about the difference between the combineLatest operator and the withLatestFrom since both of these emit the latest values. But the point of differences are:

combineLatest

does not wait for each of the input streams to emit at least one value.

withLatestFrom
withLatestFrom

withLatestFrom

only emits once all the input streams have emitted one value each.

withLatestFrom
withLatestFrom


Previous Article
Compare Leaf Traversal
Next Article
Calendar Matching
Nisar

Nisar

Related Posts

Exception handling with combination operators
May 20, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media