portfolio / notes / rxjs-combining-streams.mdoc

combineLatest, withLatestFrom, zip or forkJoin?

Two streams, four ways to combine. The same experiment through combineLatest, withLatestFrom, zip and forkJoin: only the rule for when values from different sources become one result changes.

3 min read

  • RxJS
  • Angular
Pairs emitted by each operator from the same two inputs: combineLatest A1,B1 at 2s, A2,B1 at 4s and A2,B2 at 5s; withLatestFrom A2,B1 at 4s; zip A1,B1 at 2s and A2,B2 at 5s; forkJoin A2,B2 at 7s.

When should values from different sources become one result? Four operators answer that question in four different ways:

  • combineLatest: any input changes.
  • withLatestFrom: one source leads.
  • zip: pair by position.
  • forkJoin: wait for the final values.

Same inputs, same timeline. Only the combination rule changes.

The experiment

Two inputs, one shared clock. All examples subscribe at 0s. Each input emits two values and then completes. There are no errors.

InputEmitsCompletes
AA1 at 1s, A2 at 4s6s
BB1 at 2s, B2 at 5s7s
012345678sAA1A2BB1B2
Time runs left to right; a vertical bar marks completion.

In the outputs, [A2,B1] means one emitted pair. The lines show events over time, not separate threads, and every diagram uses the same scale.

combineLatest: any input can trigger

Wait for both inputs to emit once. Then emit their latest values whenever either one changes.

combineLatest([a$, b$]);
012345678sAA1A2BB1B2Out[A1,B1][A2,B1][A2,B2]
Values are pairs: each dot on Out is one emission.

First result at 2s; further results at 4s and 5s. No initial value from one input? No combined value yet. Here, the output completes at 7s, after both inputs. Use it for a view model built from several changing inputs.

withLatestFrom: one source leads

Only A can trigger. B updates the stored value; it never triggers an output on its own. A is the source here.

a$.pipe(withLatestFrom(b$));
012345678sAA1A2BB1B2Out[A2,B1]
Only A's emissions can produce an output.

A1 is skipped: B has not emitted yet. It is not queued. At 4s, A2 uses B1. B2 at 5s produces no output. Here, the output completes with A at 6s. Use it for a submit event that reads the latest form state.

Careful with forms: valueChanges does not emit the current value when you subscribe. If the user saves before changing anything, form.valueChanges has no value yet, and the click is skipped just like A1. Start the stream with the form's current value:

const form$ = this.form.valueChanges.pipe(startWith(this.form.value));

submit$.pipe(withLatestFrom(form$)).subscribe(([, value]) => save(value));

If you only need the value at the moment of the click, you can also read it then: submit$.pipe(map(() => this.form.getRawValue())), which includes disabled controls.

zip: first with first

Wait until every input has a value for the next pair. Consume each value once, in input order.

zip(a$, b$);
012345678sAA1A2BB1B2Out[A1,B1][A2,B2]
Each value is used in exactly one pair.

A2 waits from 4s to 5s for B2. It does not reuse B1. At 6s, A is complete and its queue is empty: done. A much faster input can build up a large queue. Use it for two sequences that must be paired by index.

forkJoin: one final snapshot

Wait for every input to emit and complete. Then emit their last values once and complete.

forkJoin([a$, b$]);
012345678sAA1A2BB1B2Out[A2,B2]
One emission, only after every input has completed.

A finishing at 6s is not enough: B finishes at 7s. An input that never completes can keep you waiting. In RxJS 7, an empty input completes the join with no value. Use it for several finite HTTP requests needed together.

Choose the trigger

Ask when to emit, then choose the tool. The same values can lead to very different outputs.

When should it emit?OperatorOutputs
Any input changescombineLatest2s, 4s, 5s
Only the source emitswithLatestFrom4s
The next pair is readyzip2s, 5s
All inputs completeforkJoin7s

These tools combine streams; they do not create threads. An unhandled error in any input fails the combined output.

Check yourself

Pick an answer to see whether it is right, and why.

Saving must use the latest form state, but a change in the form alone must not save anything. Which operator replaces ????
submit$.pipe(???(form$)).subscribe(([, form]) => save(form));

With withLatestFrom, only the source (submit$) triggers an output; form$ just updates the stored value. It needs a value before the first click, so start it with the form's current value.

A dashboard's view model must update whenever the filters or the data change. Which operator?

combineLatest emits the latest values of all inputs whenever any of them changes, once each has emitted at least once.

A page needs three finite HTTP requests, all of them, before it renders. Which operator?

forkJoin waits for every input to emit and complete, then emits their last values once: one final snapshot.

In the experiment (A1 at 1s, B1 at 2s), why is there no output at 1s?
a$.pipe(withLatestFrom(b$));

When A1 arrives, B has no value to read, so A1 produces nothing. It is not queued: the first output is A2 with B1, at 4s.

You pass forkJoin an input that never completes, like a form's valueChanges. What happens?

forkJoin emits only after every input completes. An input that never completes keeps it waiting.

Sources: the RxJS 7 API for combineLatest, withLatestFrom, zip and forkJoin.

Have a product in mind? Let’s talk.

An idea on a napkin, a process that slows your team down, a product that needs another pair of hands: tell me about it. I read every message and answer with honest questions, not a sales pitch.