Consider an Angular service method that uses catchError(() => EMPTY). What will the subscriber receive if an error occurs?
import { EMPTY } from 'rxjs'; import { catchError } from 'rxjs/operators'; this.http.get('/api/data').pipe( catchError(() => EMPTY) ).subscribe({ next: data => console.log('Data:', data), error: err => console.log('Error:', err), complete: () => console.log('Completed') });
Think about what EMPTY observable does in RxJS.
The EMPTY observable emits no items and immediately completes. Using catchError(() => EMPTY) swallows the error and completes the stream silently without emitting data or error.
Which option correctly uses catchError to return a fallback observable emitting the string 'fallback' on error?
import { of } from 'rxjs'; import { catchError } from 'rxjs/operators'; this.http.get('/api/data').pipe( // catchError usage here ).subscribe(data => console.log(data));
The fallback must be an observable, not a plain value.
catchError expects a function returning an observable. of('fallback') creates an observable emitting the string 'fallback'. Options A and B return plain strings, causing errors. Option B references an undefined variable fallback.
Given this code snippet, why does the error still propagate to the subscriber's error handler?
this.http.get('/api/data').pipe( catchError(err => { console.error('Error caught:', err); throw err; }) ).subscribe({ next: data => console.log('Data:', data), error: err => console.log('Subscriber error:', err) });
What happens if you throw inside a catchError callback?
Throwing an error inside catchError re-emits the error, causing the observable to error again and the subscriber's error handler to be called. To handle the error and prevent propagation, catchError must return a new observable.
What will be logged to the console when this code runs?
import { of, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; of('start').pipe( catchError(() => of('fallback')) ).subscribe({ next: val => console.log('Next:', val), error: err => console.log('Error:', err), complete: () => console.log('Complete') }); throwError(() => new Error('fail')).pipe( catchError(() => of('fallback')) ).subscribe({ next: val => console.log('Next:', val), error: err => console.log('Error:', err), complete: () => console.log('Complete') });
Remember that of emits values and completes, and throwError emits an error.
The first observable emits 'start' and completes normally. The second observable errors, but catchError catches it and returns of('fallback'), which emits 'fallback' and completes. So the logs show 'Next: start', 'Complete', then 'Next: fallback', 'Complete'.
In Angular services using RxJS, why should you use catchError instead of a traditional try-catch block to handle HTTP errors?
Think about how asynchronous code and observables work in Angular.
All statements are true: HTTP calls return observables which are asynchronous. try-catch cannot catch errors inside observables. catchError handles errors inside the observable stream and can recover or replace the stream, making it the correct choice.