New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: clearly explain if/when we need to unsubscribe from HttpClient calls #46542
Comments
Feedback received from engineering:
|
@bob-watson that's great and as I wrote above this is exactly what I recommend my team to do, but can we please have it written in the documentation, instead of just having the generic mention of the |
Related to this topic, another pain point that I struggled to convince my team about is to never subscribe to such observables in (singleton) services, at least not without an extra mechanism to unsubscribe from the previous observable before creating a new one. I suppose this should be obvious coming from the recommendation to always unsubscribe when a component is destroyed, but again, would be nice to clarify in the documentation. |
Under "Starting the request", added the following note: You should always unsubscribe from an observable when a component is destroyed. You can use the |
@pkozlowski-opensource sorry, I don't think I understand how this is solving the issue here. It's been stated that the documentation of HttpClient is not clear regarding the need to unsubscribe. |
@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 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? |
Yes, there should be definitely more information on what to expect from the observables exposed by angular services, especially the httpClient. Do docs even mention the fact that an http request auto completes? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Description
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 theAsyncPipe
, 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.
The text was updated successfully, but these errors were encountered: