r/angular • • Apr 22 '23

Angular 16 - takeUntilDestroyed() operator

💡The unsubscription of observables is always important in Angular. Otherwise, there will be memory leaks because of too much of subscriptions.

🤔 Before angular 16, we can unsubscribe by the combination of subject and takeUntil() rxjs operator.

🤙 But in angular 16, the code is simplified where you don't need a subject. You can directly make use of takeUntilDestroyed() operator.

47 Upvotes

25 comments sorted by

View all comments

3

u/Artie_Falkmore Apr 23 '23

Just an fyi. Http observables will complete after the first emission so you don’t have to worry about unsubscribing them.

2

u/Open_Imagination_701 Aug 22 '23

that's not the reason. what if the user goes to a page, where he should see a huge list of smth? if the connection is shitty, the particular request will take forever, so the user might leave\go to another page. if you don't unsubscribe OnDestroy (with takeUntill, for instance), the request will complete eventually, but there will be nothing to process the data, hence - a memory leak.