Bird
0
0

Given the code below, what will be logged to the console?

medium📝 Predict Output Q13 of 15
Angular - RxJS Operators
Given the code below, what will be logged to the console?
const source$ = of('A', 'B', 'C');
source$.pipe(
  switchMap(letter => of(letter + '1', letter + '2'))
).subscribe(console.log);
A"C1" and "C2" only
B["A1", "A2", "B1", "B2", "C1", "C2"]
C"A1" and "A2" only
DError: switchMap requires an observable
Step-by-Step Solution
Solution:
  1. Step 1: Understand source observable emissions

    The source$ emits 'A', then 'B', then 'C' synchronously.
  2. Step 2: Apply switchMap to each emission

    For each letter, switchMap maps to an observable emitting letter+'1' and letter+'2'. Since source$ emits synchronously, switchMap cancels previous inner observables when a new outer emission occurs, so only the last inner observable's values "C1" and "C2" are emitted.
  3. Step 3: Resulting output

    Only "C1" and "C2" are logged to the console.
  4. Final Answer:

    "C1" and "C2" only -> Option A
  5. Quick Check:

    switchMap cancels previous inner observables, so only last inner observable outputs [OK]
Quick Trick: switchMap cancels previous inner observables, outputs only latest inner values [OK]
Common Mistakes:
MISTAKES
  • Thinking switchMap flattens all inner observables in order
  • Expecting all inner observable outputs
  • Confusing switchMap with mergeMap behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes