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.
- RxJS
- Angular

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.
| Input | Emits | Completes |
|---|---|---|
| A | A1 at 1s, A2 at 4s | 6s |
| B | B1 at 2s, B2 at 5s | 7s |
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$]);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$));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$);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$]);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? | Operator | Outputs |
|---|---|---|
| Any input changes | combineLatest | 2s, 4s, 5s |
| Only the source emits | withLatestFrom | 4s |
| The next pair is ready | zip | 2s, 5s |
| All inputs complete | forkJoin | 7s |
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.
Sources: the RxJS 7 API for combineLatest, withLatestFrom, zip and forkJoin.