0
0
Angularframework~10 mins

switchMap for flattening in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - switchMap for flattening
Outer Observable emits value
switchMap receives value
Cancel previous inner Observable if active
Subscribe to new inner Observable
Inner Observable emits values
Emit inner values downstream
Repeat on next outer emission
switchMap listens to an outer Observable, cancels any previous inner Observable, and switches to a new inner Observable, flattening the emissions.
Execution Sample
Angular
outer$.pipe(
  switchMap(value => inner$(value))
).subscribe(result => console.log(result));
This code listens to outer$, switches to a new inner$ Observable for each outer value, and logs inner emissions.
Execution Table
StepOuter Observable EmissionAction by switchMapInner Observable SubscribedInner EmissionOutput Emitted
1ASubscribe to inner$(A)inner$(A)11
2inner$(A) emits 2inner$(A)22
3BCancel inner$(A), subscribe inner$(B)inner$(B)
4inner$(B) emits 10inner$(B)1010
5inner$(B) emits 20inner$(B)2020
6CCancel inner$(B), subscribe inner$(C)inner$(C)
7inner$(C) emits 100inner$(C)100100
8inner$(C) completesinner$(C)
9outer$ completes
💡 outer$ completes and all inner Observables have completed or been cancelled
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 6Final
Current Inner Observablenoneinner$(A)inner$(B)inner$(C)none
Output Emittednone11,21,2,10,201,2,10,20,100
Key Moments - 2 Insights
Why does switchMap cancel the previous inner Observable when a new outer value arrives?
Because switchMap only cares about the latest outer emission, it cancels the previous inner Observable to avoid emitting outdated values, as shown in steps 3 and 6 where previous inner Observables are cancelled.
What happens to inner Observable emissions after switchMap switches to a new inner Observable?
Emissions from the old inner Observable stop being emitted downstream because it was cancelled; only the new inner Observable's emissions are forwarded, as seen in step 3 cancelling inner$(A) and step 6 cancelling inner$(B).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output emitted at step 4?
A2
B10
C20
D100
💡 Hint
Check the 'Output Emitted' column at step 4 in the execution_table.
At which step does switchMap cancel the inner Observable subscribed to inner$(A)?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the step where the action mentions cancelling inner$(A) in the execution_table.
If the outer Observable emitted a new value after step 8, what would switchMap do?
AEmit the last inner Observable's last value again
BSubscribe to a new inner Observable and cancel any previous one
CIgnore the new value because outer$ completed
DThrow an error
💡 Hint
Recall switchMap behavior from the concept_flow and how it reacts to new outer emissions.
Concept Snapshot
switchMap listens to an outer Observable.
When a new outer value arrives, it cancels the previous inner Observable.
It subscribes to a new inner Observable created from the outer value.
Only emissions from the latest inner Observable are forwarded.
Useful for flattening and switching streams dynamically.
Full Transcript
switchMap is an operator in Angular that listens to an outer Observable. When the outer Observable emits a value, switchMap cancels any previous inner Observable it was subscribed to and subscribes to a new inner Observable created from the latest outer value. This means only the latest inner Observable's emissions are passed downstream. For example, if the outer Observable emits values A, B, and C, switchMap will subscribe to inner$(A), then cancel it when B arrives and subscribe to inner$(B), then cancel inner$(B) when C arrives and subscribe to inner$(C). This behavior helps avoid outdated data and keeps the stream focused on the latest information.