question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docs: clearly explain if/when we need to unsubscribe from HttpClient calls

See original GitHub issue

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 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:closed
  • Created a year ago
  • Reactions:10
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bob-watsoncommented, Aug 4, 2022

Feedback received from engineering:

Short answer: always unsubscribe. Slightly longer answer: It’s easier to just always unsubscribe than try to learn exactly when you need to. 👍 and when you need to can be super subtle and dependent on interceptors, etc. Best practice is to always unsubscribe when a component is destroyed.

1reaction
digeomelcommented, Aug 13, 2022

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it necessary to unsubscribe from observables created by ...
So the answer is no, you don't. Ng2 will clean it up itself. The Http service source, from Angular's Http XHR backend source:....
Read more >
Do we need to unsubscribe HTTP client in Angular? | by Luka O
So whenever response comes from AJAX request, It will call the next method that's how we are getting a response back inside our...
Read more >
Do We Need to Unsubscribe Angular HttpClient? - GeekTantra
Fortunately, yes we have! But we don't need to unsubscribe it. Instead, HttpClient observes until getting a response from API. And, once it...
Read more >
Hibernate Search 6.1.7.Final: Reference Documentation
Get a Hibernate Search session, called SearchSession , from the EntityManager . 2, Create an "indexer", passing the entity types you ...
Read more >
Vert.x Core Manual
At the heart of Vert.x is a set of Java APIs that we call Vert.x Core ... and you want to handle 10000...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found