Combination Operators II

combineLatest Operator


        // synchronous behaviour
        const s1$ = rxjs.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        const s2$ = rxjs.of('a', 'b', 'c', 'd', 'e');
        rxjs.combineLatest(s1$, s2$)
            .pipe(rxjs.operators.delay(3000))
    

        // Asynchronous behaviour
        const interval$ = rxjs.interval(1000)
        rxjs.combineLatest(interval$, interval$.pipe(rxjs.operators.take(2)))
    
Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula || When any observable emits a value, emit the latest value from each

forkJoin Operator


        rxjs.forkJoin(
            rxjs.of('Hello'),   // emit 'Hello' immediately
            rxjs.of('World').pipe(rxjs.operators.delay(1000)), // emit 'World' after 1 second
            rxjs.interval(1000).pipe(rxjs.operators.take(1)), // emit 0 after 1 second
            rxjs.interval(1000).pipe(rxjs.operators.take(2)) //emit 0...1 in 1 second interval
          )
    

Pretty much like a Promise.all() but for stream so just keep last value before strem completion here

Wait for Observables to complete and then combine last values they emitted || When all observables complete, emit the last emitted value from each

zip Operator


        const interval$ = rxjs.interval(1000)
        //when one observable completes no more values will be emitted
        rxjs.zip(interval$, interval$.pipe(rxjs.operators.take(2)))
    

withLatestFrom Operator


        const source$ = rxjs.interval(2000) // emit every 2s
        const secondSource$ = rxjs.interval(500) // emit every 0.5s
        source$.pipe(
            rxjs.operators.withLatestFrom(secondSource$),
            map(([first, second]) => `First Source (5s): ${first} Second Source (1s): ${second}`)
        )
        
Whenever the source Observable emits a value, it computes a formula using that value plus the latest values from other input Observables, then emits the output of that formula || Also provide the last value from other observables

exhaust Operator


        const clicks$ = rxjs.fromEvent(document, 'click')
        const higherOrder$ = clicks$.pipe(
            rxjs.operators.map((ev) => rxjs.interval(1000).pipe(rxjs.operators.take(5)))
        )
        higherOrder$.pipe(rxjs.operators.exhaust())
    
Flattens an Observable-of-Observables by dropping the next inner Observables while the current inner is still executing

Other Combination Operators