{"id":942,"date":"2023-05-12T05:00:00","date_gmt":"2023-05-12T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=942"},"modified":"2023-05-11T14:56:05","modified_gmt":"2023-05-11T21:56:05","slug":"mergemap-rxjs-operator","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/mergemap-rxjs-operator\/","title":{"rendered":"mergeMap RxJs operator"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A few months back, we covered the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-switchmap-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>switchMap<\/code> operator<\/a> from RxJs. Today, let&#8217;s look at a similar operator called <code>mergeMap<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"394\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-12.png\" alt=\"\" class=\"wp-image-943\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-12.png 1000w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-12-300x118.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-12-768x303.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The marble diagram isn&#8217;t as obvious as it is for other operators, so let&#8217;s clarify what <code>mergeMap<\/code> does:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It takes values from an observable and maps them to another observable<\/li>\n\n\n\n<li>Then it allows us to combine the values from both observables into one new observable<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What&#8217;s a real-life use case for <code>mergeMap<\/code>?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you want to display a list of products with their associated categories. The product information is stored in one API endpoint, and the category information is stored in another API endpoint. To display the list of products with their categories, you need to make a separate API call for each product to retrieve its category information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use <code>mergeMap<\/code> to combine the two API calls and merge the results into a single stream of data (product + category).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How is it different from <code>switchMap<\/code>?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mergeMap<\/code> differs from <code>switchMap<\/code> in two important ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>switchMap<\/code> only returns the data from observable #2 &#8211; <code>mergeMap<\/code> returns data from both observable #1 and #2 and allows us to combine that data any way we want<\/li>\n\n\n\n<li><code>switchMap<\/code> cancels observable #2 whenever observable #1 emits a new value, then re-creates another observable #2. <code>mergeMap<\/code> doesn&#8217;t do that.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, <code>switchMap<\/code> is perfect when we want to chain asynchronous actions such as &#8220;user selects a new filter, then we request new data according to that filter&#8221; because if the user changes that filter again, we want to cancel the previous request and start a new one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few months back, we covered the switchMap operator from RxJs. Today, let&#8217;s look at a similar operator called mergeMap: The marble diagram isn&#8217;t as obvious as it is for other operators, so let&#8217;s clarify what mergeMap does: What&#8217;s a real-life use case for mergeMap? Let&#8217;s say you want to display a list of products [&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-942","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\/942","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=942"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/942\/revisions\/948"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}