Angular · Middle · Question 20 of 75

Why should catchError be placed inside switchMap in a long-lived stream?

this.query$.pipe(
  switchMap((q) => this.api.search(q).pipe(catchError(() => of([])))),
);

Explanation

If the error reaches the outer stream, it terminates and the search box stops working after the first failure. Handle errors per request and return a fallback value.

Further reading

Problem with a question

Angular · Middle · question 20

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz