'How many subscriptions will be created after several method calls

I'm just learning Angular. And I noticed that method subscriptions are very often used here. For example:

getUser(id: number) {
  this.userService.getUser(id).subscribe(
    (resultService: DataResponse<User>) => {       
    ...
  });
}

To get a specific user, we subscribe to this method. Let's say someone has requested different users ten times. So the getUser function was called ten times. And what happens to subscriptions in this case? The source data is different every time. Will we get ten different subscriptions? Or it will be one subscription and each time this function is called, it will be recreated.

And one more question. I have seen many similar examples, even on the Angular website itself. But I have never seen anyone unsubscribe - unsubscribe. Isn't it necessary to unsubscribe after calling the getUser method of the UserService service?



Solution 1:[1]

Assuming your getUser method uses Angular's HttpClient, you don't need to unsubscribe because Angular does that automatically for each http request.

That's the reason every example you see in the Angular website does not implement unsubscribe. HttpClient is one of the few exceptions to the rule of "always make sure to unsubscribe".

So to answer your question, you don't end up with 10 different subscriptions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JuanDeLasNieves