Angular · Senior · Question 49 of 75

Why are nested subscribe calls considered an anti-pattern?

this.route.paramMap.subscribe((p) => {
  this.api.load(p.get("id")!).subscribe((data) => (this.data = data));
});

Explanation

paramMap.pipe(switchMap((p) => this.api.load(p.get('id')!))) cancels stale requests when the param changes and gives one subscription to manage (or none, with async/toSignal).

Further reading

Problem with a question

Angular · Senior · question 49

Test yourself

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

Take this quiz