{"id":853,"date":"2023-04-25T05:00:00","date_gmt":"2023-04-25T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=853"},"modified":"2023-04-24T15:07:55","modified_gmt":"2023-04-24T22:07:55","slug":"anti-pattern-subscription-within-a-subscription","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/anti-pattern-subscription-within-a-subscription\/","title":{"rendered":"Anti-pattern: Subscription within a subscription"},"content":{"rendered":"\n<p>Welcome to this new series on Angular anti-patterns. I intend to cover one anti-pattern per week for a few weeks, starting with one of the most common ones: Subscribing to an Observable within another Observable subscription. Here&#8217;s an example where we get a country and then the currency for that country:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"146\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-42.png\" alt=\"\" class=\"wp-image-855\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-42.png 556w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-42-300x79.png 300w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Why is this a bad idea? Here are a few reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getCurrencyForCountry()<\/code> is never unsubscribed, which can lead to <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-avoid-memory-leaks-with-rxjs-observables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">memory leaks<\/a>.<\/li>\n\n\n\n<li>When the country changes, the Observable that gets the previous country&#8217;s currency is not canceled. If that Observable emits again, <code>this.currency<\/code> will get overwritten.<\/li>\n\n\n\n<li>Such nested callbacks are hard to read, and as a result, such code is more challenging to maintain.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s imagine our component template is as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"48\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-43.png\" alt=\"\" class=\"wp-image-856\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Then in a scenario where <code>getCurrencyForCountry()<\/code> emits updates values every second, our component would end up displaying the following values, which become wrong very quickly:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"231\" height=\"71\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/quiz.gif\" alt=\"\" class=\"wp-image-857\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can take a <a href=\"https:\/\/stackblitz.com\/edit\/angular-zrra7h?file=src%2Fdata.service.ts,src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">look at that code here<\/a>.<\/p>\n\n\n\n<p>The solution to avoid such issues is to use the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-switchmap-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>switchMap<\/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=\"486\" height=\"79\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-44.png\" alt=\"\" class=\"wp-image-858\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-44.png 486w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-44-300x49.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code works fine because <code>switchMap<\/code> cancels the previous currency Observable as soon as a new country is emitted from <code>service.getCountries()<\/code>. No more memory leaks, and our two Observables are always in sync:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"231\" height=\"71\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/switchmap.gif\" alt=\"\" class=\"wp-image-859\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Here is a link to that same <a href=\"https:\/\/stackblitz.com\/edit\/angular-hbp6uq?file=src%2Fdata.service.ts,src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">code example using the switchMap operator<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to this new series on Angular anti-patterns. I intend to cover one anti-pattern per week for a few weeks, starting with one of the most common ones: Subscribing to an Observable within another Observable subscription. Here&#8217;s an example where we get a country and then the currency for that country: Why is this a [&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,21],"tags":[],"class_list":["post-853","post","type-post","status-publish","format-standard","hentry","category-angular","category-anti-patterns","category-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/853","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=853"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/853\/revisions\/863"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}