{"id":949,"date":"2023-05-15T05:00:00","date_gmt":"2023-05-15T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=949"},"modified":"2023-05-12T15:10:40","modified_gmt":"2023-05-12T22:10:40","slug":"anti-pattern-series-not-unsubscribing-from-observables","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/anti-pattern-series-not-unsubscribing-from-observables\/","title":{"rendered":"Anti-pattern series: Not unsubscribing from Observables"},"content":{"rendered":"\n<p>We covered this in our newsletter before: <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-avoid-memory-leaks-with-rxjs-observables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Observables can cause memory leaks if we don&#8217;t unsubscribe<\/a> from them.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"376\" height=\"111\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-13.png\" alt=\"\" class=\"wp-image-950\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-13.png 376w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-13-300x89.png 300w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Before Angular 16, there were a <a href=\"https:\/\/blog.angulartraining.com\/how-to-automatically-unsubscribe-your-rxjs-observables-tutorial-2f98b0560298?_ga=2.55716898.808108290.1683820060-1776647484.1576363308\" target=\"_blank\" rel=\"noopener\" title=\"\">few different techniques available<\/a> to unsubscribe automatically, the best of those being the <code>async<\/code> pipe (and yes, it&#8217;s possible to use the <code>async<\/code> pipe 100% of the time if you use the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/async-pipe-syntax-tricks\/\" target=\"_blank\" rel=\"noopener\" title=\"\">tricks highlighted here<\/a> &#8211; no excuses).<\/p>\n\n\n\n<p>With Angular 16, things are even better, as you can use the new <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-16-preview-takeuntildestroyed\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>takeUntilDestroyed<\/code> operator<\/a> as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"159\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-14.png\" alt=\"\" class=\"wp-image-951\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-14.png 620w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-14-300x77.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>But here&#8217;s my million-dollar tip for today: Instead of using <code>takeUntilDestroyed<\/code> in your components, use it in the services that expose such Observables:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"178\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-15.png\" alt=\"\" class=\"wp-image-953\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-15.png 524w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-15-300x102.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>That way, whether you use an <code>async<\/code> pipe or not, your components are covered. That&#8217;s one of the nice things about Observables: We can change them whenever and wherever we want using <code>pipe()<\/code>, including in our services &#8211; so that all subscribers benefit from that change downstream.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We covered this in our newsletter before: Observables can cause memory leaks if we don&#8217;t unsubscribe from them. Before Angular 16, there were a few different techniques available to unsubscribe automatically, the best of those being the async pipe (and yes, it&#8217;s possible to use the async pipe 100% of the time if you use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,28,10],"tags":[],"class_list":["post-949","post","type-post","status-publish","format-standard","hentry","category-angular","category-anti-patterns","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=949"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/949\/revisions"}],"predecessor-version":[{"id":955,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/949\/revisions\/955"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}