Bird
0
0

Given the code below, what will be the output?

medium📝 component behavior Q13 of 15
Angular - RxJS Operators
Given the code below, what will be the output?
import { of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

const source$ = throwError(() => new Error('Oops!'));

source$.pipe(
  catchError(err => of('Error handled'))
).subscribe(value => console.log(value));
AThrows an error and stops
BNo output
CLogs 'Oops!'
DLogs 'Error handled'
Step-by-Step Solution
Solution:
  1. Step 1: Understand throwError and catchError interaction

    throwError creates an Observable that immediately errors. catchError catches this error and returns a new Observable emitting 'Error handled'.
  2. Step 2: Trace the subscription output

    The subscriber receives 'Error handled' and logs it. The original error is replaced by the catchError Observable.
  3. Final Answer:

    Logs 'Error handled' -> Option D
  4. Quick Check:

    catchError replaces error with new value [OK]
Quick Trick: catchError replaces error with new Observable output [OK]
Common Mistakes:
MISTAKES
  • Expecting original error to be logged
  • Thinking error stops the stream without catchError
  • Confusing error message with emitted value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes