RxJS Operators for Dummies: forkJoin, zip, combineLatest, withLatestFrom

If you are confused about the differences between forkJoin, zip, combineLatest and withLatestFrom, you are not alone! :)

These 4 operators are what we know as combination operators - we use them when we need to join information from multiple observables.

Explain Operators with T-Shirts!

Imagine you are printing t-shirts. Ms. Color holds the color information and Mr. Logo holds the logo information. Both of them will pick color and logospontaneously. You will need to wait and combine these two information continuously in order to print t-shirts. Ms. Color and Mr. Logo represent two observables in our code - color$ and logo$.

// 0. Import Rxjs operators
import { forkJoin, zip, combineLatest, Subject } from 'rxjs';
import { withLatestFrom, take, first } from 'rxjs/operators';

// 1. Define shirt color and logo options
type Color = 'white' | 'green' | 'red' | 'blue';
type Logo = 'fish' | 'dog' | 'bird' | 'cow';

// 2. Create the two persons - color and logo observables,
// They will communicate with us later (when we subscribe)
const color$ = new Subject<Color>();
const logo$ = new Subject<Logo>();

// 3. We are ready to start printing shirt. Need to subscribe to color and logo observables to produce shirts, we will write code here later

// 4. The two persons(observables) are doing their job, picking color and logo




// 5. When the two persons(observables) has no more info, they said bye bye.. We will write code here later

Take note of the sequence of information (part 4 in our code), here is the summary:-

Later, we will update our code (part 3 & 5) to subscribe to both color and logo observables using the 4 different operators to see how the shirts are produced differently.

Excited? Please read the full article👇in scotch.io (free of course, with better syntax highlight).