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));
});- ✓Correct answer: Inner subscriptions aren't cancelled or cleaned up, causing races and leaks; use a flattening operator
- 2RxJS forbids subscribing inside callbacks
- 3It makes the request synchronous
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).