docs: clearly explain if/when we need to unsubscribe from HttpClient calls
See original GitHub issueDescription
Hello, I’ve been going through the documentation of the HttpClient
service (https://angular.io/guide/http) trying to find any mention of whether it is necessary to unsubscribe from the generated observables or not, and the only mention I could find was about using the AsyncPipe
, which is irrelevant (because we don’t have to or might not want to use it).
So, I think the documentation should clarify when (if ever) we need to unsubscribe from calls.
Update: Perhaps I should clarify a bit more. This article: https://medium.com/angular-in-depth/why-you-have-to-unsubscribe-from-observable-92502d5639d0 explains very well the reasons why we should unsubscribe from all HttpClient observables always. Personally, I’ve struggled to convince my team to do it, because their usual answer is that HttpClient will do it automatically and of course very few people bother to read beyond the official documentation which doesn’t mention anything about unsubscribing. I have been doing it for a long time with Net Basal’s excellent ng-neat/until-destroy library, but I feel that this is such a core concept of any real-life Angular app that it should be clearly documented.
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:8 (2 by maintainers)
Top GitHub Comments
Feedback received from engineering:
@Tomassito @pkozlowski-opensource @ileil I would word it slightly different, explaining why always unsubscribing is important:
You should always unsubscribe from observables, even those created by the HttpService, because of edge cases (e.g. slow network) where the built-in unsubscribe mechanism may not kick in when the component is destroyed. When the observable is used in a component, you can use the
AsyncPipe
in the component template to achieve this.But the line added by @ileil and the clarifications provided here are already an improvement 😄 👍🏼 Maybe we can link to this discussion from the docs?