{"id":228,"date":"2023-01-03T10:10:52","date_gmt":"2023-01-03T18:10:52","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=228"},"modified":"2023-01-03T10:28:22","modified_gmt":"2023-01-03T18:28:22","slug":"rxjs-switchmap-operator","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-switchmap-operator\/","title":{"rendered":"RxJs switchMap operator"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A common mistake made in Angular applications is to nest observable subscriptions in the following manner:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">observable1.subscribe(<span class=\"hljs-function\"><span class=\"hljs-params\">data<\/span> =&gt;<\/span> {\n   observable2.subscribe(<span class=\"hljs-function\"><span class=\"hljs-params\">otherData<\/span> =&gt;<\/span> {\n     <span class=\"hljs-comment\">\/\/ Do something with otherData<\/span>\n   });\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The above syntax is not recommended because it is hard to read and can lead to subtle bugs and unexpected side effects. For instance, that syntax makes it difficult to unsubscribe properly from all these observables. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if <code>observable1<\/code> emits more than once in a short amount of time, we might want to cancel the previous subscription to <code>observable2<\/code> and start a new one based on the new data received from <code>observable1<\/code>. The above code does not do any of that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution: Use the RxJs <code>switchMap<\/code> operator like so:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">observable1.pipe(<span class=\"hljs-function\"><span class=\"hljs-params\">data<\/span> =&gt;<\/span> {\n   switchMap(<span class=\"hljs-function\"><span class=\"hljs-params\">data<\/span> =&gt;<\/span> observable2)\n).subscribe(<span class=\"hljs-function\"><span class=\"hljs-params\">otherData<\/span> =&gt;<\/span> {\n     <span class=\"hljs-comment\">\/\/ Do something with otherData<\/span>\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <code>switchMap<\/code> operator does all of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatically cancel and unsubscribe from <code>observable2<\/code> if <code>observable1<\/code> emits a new value.<\/li>\n\n\n\n<li>Automatically unsubscribes from <code>observable2<\/code> if we unsubscribe from <code>observable1<\/code><\/li>\n\n\n\n<li>Makes sure that <code>observable1<\/code> and <code>observable2<\/code> happen in sequence, one after the other. \n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following marble diagram is a good illustration of what <code>switchMap<\/code> does:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1004\" height=\"409\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image.png\" alt=\"\" class=\"wp-image-262\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image.png 1004w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-300x122.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-768x313.png 768w\" sizes=\"auto, (max-width: 1004px) 100vw, 1004px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re not using <code>switchMap<\/code> yet, look in your code for nested calls to <code>.subscribe()<\/code>, and you can start replacing those with the <code>switchMap<\/code> operator to prevent memory leaks and bugs and make your code more readable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a link to <a href=\"https:\/\/stackblitz.com\/edit\/at-rxjs-demo7?file=src%2Fapp%2Fexchange-rate.service.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">an example that uses a timer to make HTTP requests every 30 seconds<\/a> to get updated data. The timer data is turned into an API call using <code>switchMap<\/code> on line 37 of the example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common mistake made in Angular applications is to nest observable subscriptions in the following manner: The above syntax is not recommended because it is hard to read and can lead to subtle bugs and unexpected side effects. For instance, that syntax makes it difficult to unsubscribe properly from all these observables. Also, if observable1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-operators","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/228","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=228"}],"version-history":[{"count":34,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":271,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/228\/revisions\/271"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}