<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eduard Lepner</title>
    <description>The latest articles on DEV Community by Eduard Lepner (@elepner).</description>
    <link>https://dev.to/elepner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2007743%2F8dd9cca9-ab12-414d-b7c2-0ca2395f2e26.png</url>
      <title>DEV Community: Eduard Lepner</title>
      <link>https://dev.to/elepner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elepner"/>
    <language>en</language>
    <item>
      <title>The Ultimate Guide to Understanding All Flavours of switchMap</title>
      <dc:creator>Eduard Lepner</dc:creator>
      <pubDate>Sat, 31 Aug 2024 20:50:21 +0000</pubDate>
      <link>https://dev.to/elepner/the-ultimate-guide-to-understanding-all-flavours-of-switchmap-27da</link>
      <guid>https://dev.to/elepner/the-ultimate-guide-to-understanding-all-flavours-of-switchmap-27da</guid>
      <description>&lt;p&gt;Thinking reactively is one of the most valuable skill that a web developer may possess, at least in my humble opinion. Fabulous library RxJS can help with it. Developing user interfaces is all about managing streams of information. Everything that happens on the page is a some kind of stream like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user inputs,&lt;/li&gt;
&lt;li&gt;networking,&lt;/li&gt;
&lt;li&gt;sensor events,&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, many people unfairly claim that &lt;code&gt;rxjs&lt;/code&gt; is too complicated, that it's hard to use and understand providing &lt;code&gt;switchMap&lt;/code&gt; and its siblings &lt;code&gt;mergeMap&lt;/code&gt;, &lt;code&gt;concatMap&lt;/code&gt; and &lt;code&gt;exhaustMap&lt;/code&gt; as scary example that should convince you how everything is unbearably overcomplicated. The phrase &lt;strong&gt;higher order observable&lt;/strong&gt; makes some juniors crumble.&lt;/p&gt;

&lt;p&gt;The official documentation about &lt;code&gt;switchMap&lt;/code&gt; operator says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sounds scary, not gonna lie. However, the definition is much more complicated than what this operator actually does.&lt;/p&gt;

&lt;p&gt;There are million articles and videos on the Internet explaining flattening operators. My favourite is &lt;a href="https://www.youtube.com/watch?v=rUZ9CjcaCEw" rel="noopener noreferrer"&gt;this one&lt;/a&gt;. &lt;a href="https://www.youtube.com/watch?v=W2LtUxsm-kg" rel="noopener noreferrer"&gt;Recent video&lt;/a&gt; from Joshua Morony is nice too. I hope that I can add my two cents here.&lt;/p&gt;

&lt;p&gt;Without further ado, let's understand what are we mapping and where the switch is. Let's follow a simple example that perfectly illustrates how those operators act.&lt;/p&gt;

&lt;p&gt;Imagine that you are the boss and you've got an assistant. During the day the boss gives different tasks to the assistant like make cup of coffee, plan a business trip, schedule a meeting with client, etc. Those tasks arrive unevenly during the working day. The assistant gets a task and decides how to fulfil it. E.g. for coffee they need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the kitchen&lt;/li&gt;
&lt;li&gt;Take a mug&lt;/li&gt;
&lt;li&gt;Pour coffee&lt;/li&gt;
&lt;li&gt;Bring it back to the boss.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each task involves steps that take place over a period of time. All in all, the boss wants to know what the assistant has been doing during the day.&lt;/p&gt;

&lt;p&gt;What can we say about those processes in terms of streams? Tasks from the boss is obviously a stream. The act of mapping the input task into sequence of actions to execute by the assistant is where the &lt;code&gt;map&lt;/code&gt; part of each operator lies. In other words, the assistant hears "Make me a cup of coffee" and maps this command into several steps to be done. &lt;/p&gt;

&lt;p&gt;What about &lt;code&gt;switch&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt;, &lt;code&gt;merge&lt;/code&gt;, &lt;code&gt;exhaust&lt;/code&gt;? As long as the assistant completes current task before the next one arrives everything is hunky-dory. However, what if assistant still works on the previous task when suddenly a new request from the boss arrives? Here's where they have to make a decision.&lt;/p&gt;

&lt;p&gt;Let's discover what options are available:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The most obvious one is to stop doing current task and &lt;code&gt;switch&lt;/code&gt; to a new one.&lt;/li&gt;
&lt;li&gt;Be a hard working person, complete the current one, once it's completed move on to the next ie &lt;code&gt;concat&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Or vice versa, pretend not to hear what the boss said and just &lt;code&gt;exhaust&lt;/code&gt; new tasks while the current one is not completed.&lt;/li&gt;
&lt;li&gt;Enter crazy mode and &lt;code&gt;merge&lt;/code&gt; everything. Pour coffee while booking tickets to business trip. But the boss should be careful with never ending tasks in this case like sorting out mail. It may ruin entire assistant's day.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's basically it. The stream of tasks where each task contains stream of actions is called &lt;strong&gt;Higher order observable&lt;/strong&gt; in terms of Rx.  The decision that the assistant took how to treat incoming tasks if the current one in not completed is called &lt;strong&gt;flattening strategy&lt;/strong&gt; and almost all possible situations are covered by the library.&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
