<?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: Jan-Niklas Wortmann</title>
    <description>The latest articles on DEV Community by Jan-Niklas Wortmann (@niklas_wortmann).</description>
    <link>https://dev.to/niklas_wortmann</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%2F197202%2F7103cc92-5177-4678-88fb-8004ae4d0a87.jpg</url>
      <title>DEV Community: Jan-Niklas Wortmann</title>
      <link>https://dev.to/niklas_wortmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niklas_wortmann"/>
    <language>en</language>
    <item>
      <title>Announcing the Alpha Release of xstate-ngx!</title>
      <dc:creator>Jan-Niklas Wortmann</dc:creator>
      <pubDate>Fri, 05 Jul 2024 15:09:43 +0000</pubDate>
      <link>https://dev.to/angular/announcing-the-alpha-release-of-xstate-ngx-9lk</link>
      <guid>https://dev.to/angular/announcing-the-alpha-release-of-xstate-ngx-9lk</guid>
      <description>&lt;p&gt;I am very excited to announce the alpha release of &lt;strong&gt;xstate-ngx&lt;/strong&gt;! This marks a significant milestone in integrating XState with Angular, and I can't wait for you to try it out and share your feedback.&lt;/p&gt;

&lt;p&gt;For now, the project is published under &lt;code&gt;xstate-ngx&lt;/code&gt;. However, we're planning to move it into the official XState monorepo once discussions are finalized and your feedback has been implemented. You can track the progress and discussions in the &lt;a href="https://github.com/statelyai/xstate/pull/4816/files" rel="noopener noreferrer"&gt;related PR&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is xstate-ngx
&lt;/h2&gt;

&lt;p&gt;You might be wondering, what is xstate-ngx?! The official xstate documentation says the following: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;XState is a state management and orchestration solution for JavaScript and TypeScript apps.&lt;br&gt;
It uses event-driven programming, state machines, statecharts, and the actor model to handle complex logic in predictable, robust, and visual ways. XState provides a powerful and flexible way to manage application and workflow state by allowing developers to model logic as actors and state machines. It integrates well with React, Vue, Svelte, and [...]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and now there is an angular integration! xstate-ngx uses the primitives that XState provides and provides a thin wrapper to utilize Angular's Dependency Injection mechanism and signals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Alpha?
&lt;/h2&gt;

&lt;p&gt;This alpha release aims to give you a taste of the developer experience with &lt;code&gt;xstate-ngx&lt;/code&gt;. I want you to play with it, explore its capabilities, and most importantly, provide feedback on the general API design. Your input is crucial in shaping the future of this integration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To help you get started, we've provided several examples on &lt;a href="https://github.com/niklas-wortmann/xstate-angular" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. Here's a quick highlight of how you can use &lt;code&gt;xstate-ngx&lt;/code&gt; in your projects:&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Simple Toggle Machine
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createMachine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;interpret&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;xstate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMachine&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;xstate-ngx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Define your machine&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggleMachine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createMachine&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toggle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inactive&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;states&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;inactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;TOGGLE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;TOGGLE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inactive&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ToggleMachineService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMachine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toggleMachine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-toggle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ToggleMachineService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
        &amp;lt;button (click)="toggleMachine.send('TOGGLE')"&amp;gt;
            {{ toggleMachine.snapshot().value === 'inactive' ? 'Off' : 'On' }}
        &amp;lt;/button&amp;gt;
    `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ToggleComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;toggleMachine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ToggleMachineService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a simple toggle state machine and create an Injectable using &lt;code&gt;useMachine&lt;/code&gt; from &lt;code&gt;xstate-ngx&lt;/code&gt;. The returned Service can then be used in a component. The &lt;code&gt;snapshot&lt;/code&gt; property is a signal allowing for fine-grained reactivity, but also enabling to easily derive state by using the &lt;code&gt;compute&lt;/code&gt; function. &lt;/p&gt;

&lt;h2&gt;
  
  
  Special Thanks
&lt;/h2&gt;

&lt;p&gt;I couldn't have reached this milestone without the invaluable contributions and support from the community:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://x.com/Enea_Jahollari" rel="noopener noreferrer"&gt;&lt;strong&gt;Enea Jahollari&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://x.com/Nartc1410" rel="noopener noreferrer"&gt;&lt;strong&gt;Chau Tran&lt;/strong&gt;&lt;/a&gt; for their first round of feedback.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://x.com/DavidKPiano" rel="noopener noreferrer"&gt;&lt;strong&gt;David Khourshid&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://x.com/AndaristRake" rel="noopener noreferrer"&gt;&lt;strong&gt;Mateusz Burzyński&lt;/strong&gt;&lt;/a&gt; for their outstanding work on XState, their interest in an Angular implementation, and their insightful discussions about this topic.&lt;/li&gt;
&lt;li&gt;The design and implementation are heavily inspired by NgRx Signals, so many thanks to the NgRx team!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Join the Conversation
&lt;/h2&gt;

&lt;p&gt;Your feedback is essential to us. Join the conversation, try out the alpha release, and let us know your thoughts. For now the best place to share any kind of feedback is the &lt;a href="https://github.com/niklas-wortmann/xstate-angular" rel="noopener noreferrer"&gt;xstate-angular repository&lt;/a&gt;. Together, we can make &lt;code&gt;xstate-ngx&lt;/code&gt; a robust and delightful tool for the Angular community.&lt;/p&gt;

&lt;p&gt;Let me know what you think and happy coding!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>xstate</category>
      <category>webdev</category>
    </item>
    <item>
      <title>An Introduction to Pact and Consumer-Driven Contract Testing</title>
      <dc:creator>Jan-Niklas Wortmann</dc:creator>
      <pubDate>Tue, 03 Nov 2020 16:42:15 +0000</pubDate>
      <link>https://dev.to/niklas_wortmann/an-introduction-to-pact-and-consumer-driven-contract-testing-4ea1</link>
      <guid>https://dev.to/niklas_wortmann/an-introduction-to-pact-and-consumer-driven-contract-testing-4ea1</guid>
      <description>&lt;p&gt;E2E Testing is difficult, definitely one of the most difficult challenges within front-end development. UI tests tend to be flaky, but depending on your risk assessment you probably want to have the confidence E2E tests provide. But have you considered Consumer-Driven-Contract Testing? It is an automated testing approach to test the API integration of distributed systems, promising fast feedback and eliminating flaky integration tests. One tool providing these benefits and enabling you to write Consumer-Driven Contract Tests is &lt;a href="https://docs.pact.io/" rel="noopener noreferrer"&gt;Pact&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spoiler:&lt;/strong&gt; This is the first part of a series of blog posts taking an in-depth look into Consumer-Driven Contract Testing with Pact particularly for JavaScript Developers. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Consumer Driven-Contract Testing?
&lt;/h2&gt;

&lt;p&gt;Splitting the word "Consumer-Driven Contract Testing" apart helps in understanding this testing approach. Let's first have a look at the term "Contract Testing". A Contract in this instance refers to some kind of specification describing the behavior of an HTTP based API. This specification can be in different formats and depends on the concrete implementation of Contract Testing. You can think of it as some kind of &lt;a href="https://editor.swagger.io/" rel="noopener noreferrer"&gt;&lt;code&gt;swagger.json&lt;/code&gt;&lt;/a&gt; or an &lt;a href="https://www.openapis.org/" rel="noopener noreferrer"&gt;OpenAPI specification&lt;/a&gt; if you are familiar with those. Usually such a contract specifies a REST-API, though it is not limited to that. So "Contract Testing" is a testing approach that uses the API specification to test the provider of the API but also the consumer of the API in isolation. We will talk about this more in detail later on in the concrete example of how Pact implements Consumer-Driven Contract Testing.&lt;br&gt;
So let's have a look at the first part of the term "Consumer-Driven Contract Testing". "Consumer-Driven" in this regard means that the API is not dictated by the creator of the API.  Usually when it comes to API design, the team that is providing the API is taking care of the design of it. Alternatively in "Consumer-Driven" Contract Testing, the consumer of the API is specifying the API according to its needs. This does not mean that the consumer alone &lt;strong&gt;dictates&lt;/strong&gt; the API. In optimal scenarios, the API specification is the result of an open discussion with having all parties' interests in mind and coming to a solution that works for every party involved. Therefore "Consumer-Driven" just refers to the process of creating an API specification.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Pact?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Pact is a code-first tool for testing HTTP and message integrations using contract tests. Contract tests assert that inter-application messages conform to a shared understanding that is documented in a contract.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Taken from the &lt;a href="https://docs.pact.io/" rel="noopener noreferrer"&gt;Pact Documentation&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In my own words: Pact is a language-agnostic framework implementing Consumer-Driven Contract testing, to verify the behavior and integration of an API by testing all relevant parties in isolation. The API specification is done in a JSON file with its very own schema. We will take a look at such a specification in the next part of this series.&lt;/p&gt;

&lt;p&gt;The following image shows how Pact implements Consumer-Driven Contract Testing on a very high level&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.pact.io%2Fassets%2Fimages%2Fpact-test-and-verify-7ae6e70a9a42ffa4ac8373ba294b19d9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.pact.io%2Fassets%2Fimages%2Fpact-test-and-verify-7ae6e70a9a42ffa4ac8373ba294b19d9.png"&gt;&lt;/a&gt;&lt;br&gt;using Pact to test Consumer and Provider in isolation by spinning up a mock server acting accordingly to the specification
  &lt;/p&gt;

&lt;p&gt;To run Consumer-Driven Contract tests with Pact on the Consumer-side you are going to spin up a mock server that is simulating the behavior of the API Provider. Therefore the Consumer is performing its request as it would be in the normal program flow. If the request is compliant to the specification, the mock-server is going to reply with the specified response. Part of this test is also to verify that the Consumer works as expected with the returned response. &lt;/p&gt;

&lt;p&gt;On the Provider-side, this looks pretty similar. In this case, the mock server Pact spins up will simulate the Consumer of the API. Pact is going to perform the request according to the contract and verifying that the server response is according to the specification. &lt;/p&gt;

&lt;p&gt;Knowing this makes it clear that both parties are tested in isolation, but by using the same contract you test the API interaction integratively. Using this testing approach gives you high confidence about the integration of your Provider and the Consumer, you get fast feedback, and you can develop the relative part on its own without being dependant on the progress of the relative other party.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Consumer-Driven Contract Testing makes sense when:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;you are having lots of inter-service communication&lt;/li&gt;
&lt;li&gt;microservices&lt;/li&gt;
&lt;li&gt;Provider is developed by a different team than the Consumer&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using Consumer-Driven Contract Testing may not make sense when:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;providing an API for external parties&lt;/li&gt;
&lt;li&gt;consuming third party APIs&lt;/li&gt;
&lt;li&gt;Provider already exists and you are reusing the same API&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Does Consumer-Driven Contact Testing replace E2E testing?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;TL;DR&lt;/em&gt; Nope. &lt;/p&gt;

&lt;p&gt;Consumer-Driven Contract Testing is a good approach in an overall testing strategy. It does not replace E2E testing as E2E Testing is business-case driven and process-oriented. E2E tests will give you great insights whether your business use cases are working as expected, but Consumer-Driven Contract Testing is solely focused on API integration. The test execution is very fast and wrong assertions can be pinpointed really well, but E2E Tests have a way bigger scope, giving you more clearance on the functionality of your overall application. &lt;/p&gt;

&lt;h2&gt;
  
  
  Outlook
&lt;/h2&gt;

&lt;p&gt;I hoped you enjoyed reading this introduction, as this is only the first part of a series of blog posts I will be writing about this topic. Upcoming in this series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Taking a look at the Pact Contract specification&lt;/li&gt;
&lt;li&gt;Integrating Pact Consumer tests in an Angular Application&lt;/li&gt;
&lt;li&gt;Implementing Pact Provider tests in a NestJS  Application &lt;/li&gt;
&lt;li&gt;Adding Pact API verification to a CI pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay tuned for the next part! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>testing</category>
      <category>pact</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Nobody has to use HostListener</title>
      <dc:creator>Jan-Niklas Wortmann</dc:creator>
      <pubDate>Wed, 18 Dec 2019 17:19:16 +0000</pubDate>
      <link>https://dev.to/angular/ain-t-nobody-needs-hostlistener-fg4</link>
      <guid>https://dev.to/angular/ain-t-nobody-needs-hostlistener-fg4</guid>
      <description>&lt;p&gt;Angular's &lt;code&gt;@hostListener&lt;/code&gt; is well known within the community. Rather unknown are the problems this might have on runtime performance and general application architecture. In general, there are three main problems with using the &lt;code&gt;hostListener&lt;/code&gt; decorator.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Missing composability&lt;/li&gt;
&lt;li&gt;Performance issues&lt;/li&gt;
&lt;li&gt;Lacks configuration options&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before tackling those two problems more in detail, let's have a look at the example code used to demonstrate the problem.&lt;br&gt;
To do so let's have a look at the following Stackblitz example, particularly the &lt;code&gt;BoxComponent&lt;/code&gt;: &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/angular-hostlistener-imperative?file=src%2Fapp%2Fbox.component.ts" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here we see an implemented drag'n'drop feature, using the &lt;code&gt;@hostListener&lt;/code&gt; decorator. In total, we registered 3 listeners.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;mousedown&lt;/code&gt; event, which we are using to set a property signalling that our drag'n'drop is about to start.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;mousemove&lt;/code&gt; event, which calculates the position of the rectangle according to the mouse position.&lt;/li&gt;
&lt;li&gt;Finally, we are using the &lt;code&gt;mouseup&lt;/code&gt; event to signal that our drag'n'drop has ended.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do note that we used &lt;code&gt;document&lt;/code&gt; as eventTarget. We needed that to handle fast mouse movements which might be out of sync with the position of the rectangle. One will notice that when moving the mouse very fast, that one is out of the rectangle element, which would stop our drag'n'drop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems
&lt;/h2&gt;

&lt;p&gt;Let's have a more in-depth look at the problems listed above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Missing composability
&lt;/h3&gt;

&lt;p&gt;Taking a look into the code, we will notice that we set the property &lt;code&gt;isClicked&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; as soon as the &lt;code&gt;mousedown&lt;/code&gt; event happens. We use that property to perform an early return inside of the &lt;code&gt;mousemove&lt;/code&gt; event handler to stop this function from execution. This is the only way we can compose those two events, which is quite expensive because this &lt;code&gt;mousemove&lt;/code&gt; function is still executed with every mouse movement. In terms of composition, this drag'n'drop feature is fairly straight forward. There are several much more complex event composition scenarios, which become extremely difficult when using the &lt;code&gt;@hostListener&lt;/code&gt; decorator.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Performance issues
&lt;/h3&gt;

&lt;p&gt;This problem is mostly the resolution of the missing composability. The problem here is that we register the 3 event listener, mentioned above, for every component instance, even though it's impossible to drag'n'drop multiple rectangles at the same time. Therefore what we should aim for is, that only the &lt;code&gt;mousedown&lt;/code&gt; event listener is registered for every component and just when this event happens we register the other events accordingly. Doing all this logic within the event listener function is a lot of work and also decently complex. Additionally, there is currently no way to disable a registers &lt;code&gt;@hostListener&lt;/code&gt; function. This is also the reason why the code example above constantly listens to mouse move events, even though they are not relevant if there isn't a rectangle selected before. &lt;/p&gt;

&lt;h3&gt;
  
  
  Lacks configuration options
&lt;/h3&gt;

&lt;p&gt;Usually, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener" rel="noopener noreferrer"&gt;&lt;code&gt;addEventListener&lt;/code&gt;&lt;/a&gt; provides an argument for configuration options (the description below is copied from the MDN web docs):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;capture:&lt;/strong&gt; A &lt;code&gt;Boolean&lt;/code&gt; indicating that events of this type will be dispatched to the registered &lt;code&gt;listener&lt;/code&gt; before being dispatched to any &lt;code&gt;EventTarget&lt;/code&gt; beneath it in the DOM tree. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;once:&lt;/strong&gt; A &lt;code&gt;Boolean&lt;/code&gt; indicating that the &lt;code&gt;listener&lt;/code&gt; should be invoked at most once after being added. If &lt;code&gt;true&lt;/code&gt;, the &lt;code&gt;listener&lt;/code&gt; would be automatically removed when invoked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;passive:&lt;/strong&gt; A &lt;code&gt;Boolean&lt;/code&gt; which, if &lt;code&gt;true&lt;/code&gt;, indicates that the function specified by &lt;code&gt;listener&lt;/code&gt; will never call &lt;code&gt;preventDefault()&lt;/code&gt;. If a passive listener does call preventDefault(), the user agent will do nothing other than generating a console warning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One can clearly see that those configuration options are very powerful. For sure, one probably don't need to use them for every case. But especially for heavily event-oriented features this configuration options are key. If we take a look at the &lt;a href="https://angular.io/api/core/HostListener" rel="noopener noreferrer"&gt;offical Angular documentation&lt;/a&gt; we will see, that we are not able to specify these configuration parameters, when using the &lt;code&gt;hostListener&lt;/code&gt; decorator.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative Approaches
&lt;/h2&gt;

&lt;p&gt;We have two different approaches to tackle the problems described above. Depending on your knowledge some of them are more or less complex. Let's have a look!&lt;/p&gt;

&lt;h3&gt;
  
  
  Using addEventListener
&lt;/h3&gt;

&lt;p&gt;Theoretically one could register nested event listeners. Therefore we could use the &lt;code&gt;addEventListener&lt;/code&gt; function to register the event listeners.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/angular-hostlistener-eventlistener?file=src%2Fapp%2Fbox.component.ts" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Looking at the code example one will notice that this is fairly complex. Especially because we need to take care of registering and unregistering the nested event listeners. Even if all of the problems described above can be solved with this approach, In my personal opinion, I think that this is a very complex and hard to understand solution. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using fromEvent
&lt;/h3&gt;

&lt;p&gt;The second alternative approach would be using the RxJS &lt;a href="https://rxjs.dev/api/index/function/fromEvent" rel="noopener noreferrer"&gt;&lt;code&gt;fromEvent&lt;/code&gt;&lt;/a&gt; operator. RxJS shines when it comes to composition of event-oriented code. &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/angular-hostlistener-reactive?file=src%2Fapp%2Fbox.component.ts" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Having a look at this code, one will notice that just looking at the lines of code that this is the smallest approach. I have to admit that one needs to be familiar with RxJS to understand and write such code. It's not really intuitive, but therefore RxJS takes care of registering and unregistering the event listener for us. Additionally, we have many more opportunities regarding composability. That's one of the key benefits of using RxJS when dealing with event-oriented code. &lt;/p&gt;

&lt;p&gt;If you want to understand the used operators you can have a look at the following blog posts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/rxjs/about-switchmap-and-friends-2jmm"&gt;switchMapTo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/rxjs/takewhile-takeuntil-takewhat-5006"&gt;takeUntil&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;@hostListener&lt;/code&gt; decorator is handy if we just want to listen to single events and don't rely on any kind of composition. Everything that involves a certain event composition should be implemented by using one of the other approaches listed above. In general, &lt;code&gt;@hostListener&lt;/code&gt; lacks features that are necessary when dealing with event composition. It completely misses &lt;em&gt;cancellation&lt;/em&gt; options and any kind of &lt;em&gt;composability&lt;/em&gt;. Those features are crucial when building heavily event-oriented features.&lt;br&gt;
When you are used to RxJS you should probably use the &lt;code&gt;fromEvent&lt;/code&gt; operator to perform any kind of complex event handling. If RxJS is not your preferred technology, maybe using plain old &lt;code&gt;addEventListener&lt;/code&gt; might be a viable option for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;This blog post aims to elaborate on different approaches to deal with event composition. It never intends to blame or hurt someone who was involved in the design or implementation of the &lt;code&gt;@hostListener&lt;/code&gt; feature. I personally appreciate any work that was put into that. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>takeWhile, takeUntil, takeWhat?</title>
      <dc:creator>Jan-Niklas Wortmann</dc:creator>
      <pubDate>Tue, 30 Jul 2019 14:06:45 +0000</pubDate>
      <link>https://dev.to/rxjs/takewhile-takeuntil-takewhat-5006</link>
      <guid>https://dev.to/rxjs/takewhile-takeuntil-takewhat-5006</guid>
      <description>&lt;p&gt;It might be know to you already, that there are situations where you actually want to unsubscribe from your &lt;code&gt;Observables&lt;/code&gt;.&lt;br&gt;
There are several ways to do this. In several projects, I stumbled across a mixture of using &lt;a href="https://rxjs.dev/api/operators/takeWhile" rel="noopener noreferrer"&gt;&lt;code&gt;takeWhile&lt;/code&gt;&lt;/a&gt; &amp;amp; &lt;a href="https://rxjs.dev/api/operators/takeUntil" rel="noopener noreferrer"&gt;&lt;code&gt;takeUntil&lt;/code&gt;&lt;/a&gt;. This raises the question, why are there two of that, which sounds similar and act similarly?&lt;/p&gt;
&lt;h2&gt;
  
  
  About takeWhile
&lt;/h2&gt;

&lt;p&gt;Well, let's take a look at a quick example. The first code snippet we are looking at is using &lt;code&gt;takeWhile&lt;/code&gt; to unsubscribe from an &lt;code&gt;Observable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/rxjs-dev-takewhile?" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this example I have two different &lt;code&gt;Observables&lt;/code&gt;. The first is created using the &lt;a href="https://rxjs.dev/api/index/function/interval" rel="noopener noreferrer"&gt;interval operator&lt;/a&gt;. This will emit notifications till the condition passed to &lt;code&gt;takeWhile&lt;/code&gt; is falsy. Inside the &lt;code&gt;takeWhile&lt;/code&gt; we use a boolean variable describing if the user has already clicked or not. As soon has one clicks somewhere in the screen, we unsubscribe from our &lt;code&gt;interval&lt;/code&gt;-Observable. To determine if the user already clicked we used a second &lt;code&gt;Observable&lt;/code&gt; created with the &lt;a href="https://rxjs.dev/api/index/function/fromEvent" rel="noopener noreferrer"&gt;fromEvent operator&lt;/a&gt;. Additionally we used the &lt;a href="https://rxjs.dev/api/operators/tap" rel="noopener noreferrer"&gt;tap operator&lt;/a&gt;, to log the notifications in the console. We can see that our Observable is unsubscribed as soon as there is no new log coming in.&lt;/p&gt;

&lt;h2&gt;
  
  
  About takeUntil
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/rxjs-dev-takeuntil?" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;From a high-level perspective, the code snippets don't look that different. Instead of having a boolean property, describing the state of our &lt;code&gt;Observable&lt;/code&gt;, we now directly used the &lt;code&gt;click&lt;/code&gt;-Observable.&lt;br&gt;
We pass this &lt;code&gt;Observable&lt;/code&gt; instance to the &lt;code&gt;takeUntil&lt;/code&gt; operator and as soon as the user clicks somewhere, our &lt;code&gt;interval&lt;/code&gt;-Observable will be unsubscribed from.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;So all in all both code snippets look similar and behave similar, right? Well, No!&lt;br&gt;
Let's have a look at the marble diagrams describing those operators because this will highlight the difference between those two operators.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Frxjs-blog%2Fblog%2Fmaster%2Fblog-posts%2Ftakeuntil-takewhile%2Fassets%2FtakeUntil.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Frxjs-blog%2Fblog%2Fmaster%2Fblog-posts%2Ftakeuntil-takewhile%2Fassets%2FtakeUntil.png" alt="takeUntil marble diagram, kindly provided by Michael Hladky"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;takeUntil marble diagram, kindly provided by &lt;a href="https://twitter.com/Michael_Hladky" rel="noopener noreferrer"&gt;Michael Hladky&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Frxjs-blog%2Fblog%2Fmaster%2Fblog-posts%2Ftakeuntil-takewhile%2Fassets%2FtakeWhile.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Frxjs-blog%2Fblog%2Fmaster%2Fblog-posts%2Ftakeuntil-takewhile%2Fassets%2FtakeWhile.png" alt="takeWhile marble diagram, kindly provided by Michael Hladky"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;takeWhile marble diagram, kindly provided by &lt;a href="https://twitter.com/Michael_Hladky" rel="noopener noreferrer"&gt;Michael Hladky&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The problem here is that &lt;code&gt;takeWhile&lt;/code&gt; is intended to take an incoming notification and check a specified condition on it, which might lead to an unsubscribe. The important fact is, that &lt;code&gt;takeWhile&lt;/code&gt; is triggered by the incoming notification and might unsubscribe afterwards. In contrast &lt;code&gt;takeUntil&lt;/code&gt; is triggered by the passed &lt;code&gt;Observable&lt;/code&gt;.&lt;br&gt;
That's why &lt;code&gt;takeWhile&lt;/code&gt; might cause several issues. So definitely, it needs a new notification to unsubscribe. Imagine having a long-living &lt;code&gt;Observable&lt;/code&gt;. You will need one notification more with &lt;code&gt;takeWhile&lt;/code&gt; than with &lt;code&gt;takeUntil&lt;/code&gt;. Also, this additional notification can initiate multiple processes within your &lt;code&gt;Observable&lt;/code&gt;. Imagine having code like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;longLivingObservable$&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startAnimation&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
    &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeHttpCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="nf"&gt;takeWhile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;alive&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So what's the issue with this piece of code? Well, our component is already destroyed and due to the needed notification, needed before unsubscribing kicks in, we will start an animation and trigger an HTTP call. This is probably unwanted and just afterwards we will check if we want to unsubscribe from our &lt;code&gt;Observable&lt;/code&gt;. Besides the fact that those operations are totally superfluous, it also might break our app or pollute our state.&lt;/p&gt;

&lt;p&gt;Additionally, if our &lt;code&gt;Observable&lt;/code&gt; doesn't emit an additional value, the &lt;code&gt;takeWhile&lt;/code&gt; will never be triggered and therefore our &lt;code&gt;Observable&lt;/code&gt; will never be unsubscribed. This can be considered as memory-leak, because our &lt;code&gt;Observable&lt;/code&gt; stays subscribed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now maybe one might argue: "Well, I could move the &lt;code&gt;takeWhile&lt;/code&gt; operator at the very beginning of the observable pipeline!"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's true, you could do this, and you will save the unneeded operations, which is a good start, but you won't unsubscribe from inner observables. So if the &lt;code&gt;Observable&lt;/code&gt; returned by &lt;code&gt;makeHttpCall&lt;/code&gt; is a long-living &lt;code&gt;Observable&lt;/code&gt;, it will not unsubscribe from that, if the &lt;code&gt;takeWhile&lt;/code&gt; is before &lt;code&gt;switchMap&lt;/code&gt; in the pipe. By the way, the same goes for &lt;code&gt;takeUntil&lt;/code&gt;, so make sure to have the unsubscribe-operator at the very end of your pipe.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Don't get me wrong, &lt;code&gt;takeWhile&lt;/code&gt; is an amazing operator, but just if you actually use the incoming value to determine, whether you want to unsubscribe or not! Do not depend on "global" state, when using &lt;code&gt;takeWhile&lt;/code&gt;.&lt;br&gt;
For those scenarios stick to &lt;code&gt;takeUntil&lt;/code&gt; and use a Subject instance to trigger it.&lt;/p&gt;

&lt;p&gt;A real-world use case for &lt;code&gt;takeWhile&lt;/code&gt; would be a long-polling mechanism. Imagine fetching a resource describing a process. This process can be successfully completed or otherwise ongoing. For sure you just want to continue polling while the process isn't completed yet. The code for such a scenario could look like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;longPolling$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;takeWhile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleNotCompleted&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For such a scenario, where we use the incoming will, to determine wether we want to stay subscribed or not, &lt;code&gt;takeWhile&lt;/code&gt; is ideal! If we have an external trigger, stick with &lt;code&gt;takeUntil&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;use takeWhile when the incoming value make you want to unsubscribe&lt;/li&gt;
&lt;li&gt;use takeUntil when there is an outer event determine that you want to unsubscribe&lt;/li&gt;
&lt;li&gt;use both of them as the last operator in your &lt;code&gt;Observable&lt;/code&gt; pipeline

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/cartant/rxjs-tslint-rules" rel="noopener noreferrer"&gt;There's a tslint rule for that&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Special Thanks
&lt;/h2&gt;

&lt;p&gt;I'm really thankful for all the amazing people helped me writing this blog posts.&lt;br&gt;
This goes out to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Jordy_Langen" rel="noopener noreferrer"&gt;Jordy Langen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Michael_Hladky" rel="noopener noreferrer"&gt;Michael Hladky&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/tim_deschryver" rel="noopener noreferrer"&gt;Tim Deschryver&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/tobmaster" rel="noopener noreferrer"&gt;Tobias Struckmeier&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/ManfredSteyer" rel="noopener noreferrer"&gt;Manfred Steyer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/esosanderelias" rel="noopener noreferrer"&gt;Sander Elias&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/wesgrimes" rel="noopener noreferrer"&gt;Wes Grimes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rxjs</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>About switchMap and friends</title>
      <dc:creator>Jan-Niklas Wortmann</dc:creator>
      <pubDate>Thu, 18 Jul 2019 22:21:07 +0000</pubDate>
      <link>https://dev.to/rxjs/about-switchmap-and-friends-2jmm</link>
      <guid>https://dev.to/rxjs/about-switchmap-and-friends-2jmm</guid>
      <description>&lt;p&gt;RxJS comes with more than 100 different operators. &lt;a href="https://rxjs.dev/api/operators/switchMap" rel="noopener noreferrer"&gt;SwitchMap&lt;/a&gt; is probably the one that is discussed the most often. It's a very powerful operator, being very useful in many situations, but it's also quite a dangerous one. In this blog article, we will talk about &lt;code&gt;switchMap&lt;/code&gt; and also covers the issues it might introduce to your application. Additionally, we will cover the operators having a similar use case. This will make it easier for you, the next time you need to pick one of those.&lt;/p&gt;

&lt;p&gt;Before we start talking about the dos and don'ts of using &lt;code&gt;switchMap&lt;/code&gt;, let's first summarize the way &lt;code&gt;switchMap&lt;/code&gt; is working. SwitchMap is a so-called higher-order operator. Maybe you are already familiar with the concept of higher-order functions because it's quite similar.&lt;br&gt;
In case you aren't, A higher-order function is a function returning another function or accepting a function as a parameter. Think about the Array method &lt;code&gt;map&lt;/code&gt;. It takes a function as a parameter which is used to define the transformation of every single item within the array.&lt;/p&gt;

&lt;p&gt;A higher-order operator is handling an Observable of Observables. &lt;code&gt;SwitchMap&lt;/code&gt; particularly is returning the notifications of an inner Observable.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://my.api.com?search=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code example will be used to explain &lt;code&gt;switchMap&lt;/code&gt; roughly. We will extend it later on, to take a deeper look at it.&lt;/p&gt;

&lt;p&gt;But first things first. &lt;code&gt;SwitchMap&lt;/code&gt; takes the values from the outer observable (returned by the &lt;a href="https://rxjs.dev/api/index/function/of" rel="noopener noreferrer"&gt;of&lt;/a&gt; operator) and pass those as a parameter to a function which has to return a new observable. In this particular case, we are using the ajax module of RxJS (for reference you can find some information &lt;a href="https://rxjs.dev/api/ajax/ajax" rel="noopener noreferrer"&gt;here&lt;/a&gt;). Here we are using the &lt;code&gt;getJSON&lt;/code&gt; method to perform a get request and return its response as a new observable.&lt;/p&gt;

&lt;p&gt;Technically, we are subscribing to the newly returned observable and passing its value to the next operator in the chain or as usual to the next handler in the subscribe method.&lt;/p&gt;

&lt;p&gt;Now you are familiar with the basics of &lt;code&gt;switchMap&lt;/code&gt;, we will take a deeper look at it. Don't worry if it may take a while to fully understand &lt;code&gt;switchMap&lt;/code&gt; in detail. But if you got it, you will notice, that &lt;code&gt;concatMap&lt;/code&gt;, &lt;code&gt;mergeMap&lt;/code&gt;, and &lt;code&gt;exhaustMap&lt;/code&gt; are quite similar. But first, let's dive deep into &lt;code&gt;switchMap&lt;/code&gt;. As promised I extended the&lt;br&gt;
example above and to make it more explorable I created a &lt;a href="https://stackblitz.com/edit/rxjs-about-switchmap?file=index.ts" rel="noopener noreferrer"&gt;Blitz&lt;/a&gt; for it.&lt;br&gt;
In the &lt;code&gt;index.ts&lt;/code&gt;, you will stumble across the following piece of code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpCall$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://rickandmortyapi.com/api/character/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;click$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;switchMapExample$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;click$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inside switchMap - click happend&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inside switchMap - start http request&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;httpCall$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inside switchMap - http response &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Instead of passing one single value to the switchMap function we are now calling switchMap every click (for reference take a look at &lt;a href="https://rxjs.dev/api/index/function/fromEvent" rel="noopener noreferrer"&gt;fromEvent&lt;/a&gt;). The &lt;code&gt;fromEvent&lt;/code&gt; is used as an example for any kind of input stream. It could also be any other &lt;code&gt;Observable&lt;/code&gt; you'd like to.&lt;br&gt;
So as soon as you click somewhere on the page it will immediately log &lt;code&gt;inside switchMap - click happened&lt;/code&gt; to the console. Afterwards, &lt;code&gt;switchMap&lt;/code&gt;is called. This will also log &lt;code&gt;inside switchMap - start http request&lt;/code&gt; immediately. At the end of the function passed to &lt;code&gt;switchMap&lt;/code&gt;, we are returning an &lt;code&gt;httpCall$&lt;/code&gt; Observable. This will perform an HTTP request as soon as someone subscribes to it. Additionally, we are using &lt;a href="https://rxjs.dev/api/operators/tap" rel="noopener noreferrer"&gt;tap&lt;/a&gt; again to log the value of the HTTP response.&lt;br&gt;
&amp;lt;&amp;gt;&lt;br&gt;
I already mentioned above, &lt;code&gt;switchMap&lt;/code&gt; is passing the value of a source &lt;code&gt;Observable&lt;/code&gt; and pass this to a function that needs to return a new &lt;code&gt;Observable&lt;/code&gt;. &lt;code&gt;SwitchMap&lt;/code&gt; will take care of subscribing to that returned Observable. But now there are two edge cases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What happens if the &lt;code&gt;Observable&lt;/code&gt; returned inside &lt;code&gt;switchMap&lt;/code&gt; is a long-living &lt;code&gt;Observable&lt;/code&gt; which emits multiple items.&lt;/li&gt;
&lt;li&gt;What happens if my source Observable emits faster than the one returned inside &lt;code&gt;switchMap&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both of these problems are visualized in the following marble diagram.&lt;br&gt;
If you are not familiar with the marble diagram syntax, take a look at the &lt;a href="https://rxjs.dev/guide/operators" rel="noopener noreferrer"&gt;Marble Diagram Section&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please notice that all the following marble diagrams are kindly provided by &lt;a href="https://twitter.com/Michael_Hladky" rel="noopener noreferrer"&gt;Michael Hladky&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8pjpdu0jkekpf1nl67l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8pjpdu0jkekpf1nl67l.jpg" alt="switchMap marble diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go through these problems one by one. In case we returned a long-living &lt;code&gt;Observable&lt;/code&gt; inside our &lt;code&gt;switchMap&lt;/code&gt; function, we will see in the marble diagram, that all notifications are outputted. A very common example for a long-living &lt;code&gt;Observable&lt;/code&gt; would be HTTP polling, where we request an API every X seconds and return its response. All those responses would be passed to the next operator in the &lt;code&gt;Observable&lt;/code&gt; chain. As you can see in the &lt;code&gt;i2$&lt;/code&gt; Observable, both notifications are passed to the &lt;code&gt;output$&lt;/code&gt; Observable. Now that we know how &lt;code&gt;switchMap&lt;/code&gt; handles long-living &lt;code&gt;Observables&lt;/code&gt;, the second edge case arises. What happens, when the source &lt;code&gt;Observable&lt;/code&gt; emits faster than the newly created &lt;code&gt;Observable&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;SwitchMap&lt;/code&gt; interrupts the &lt;code&gt;Observable&lt;/code&gt; execution as soon as there is a new value coming from the source &lt;code&gt;Observable&lt;/code&gt;. If you double-check this with the marble diagram, you will notice that the stream &lt;code&gt;i1$&lt;/code&gt; immediately ends as soon as notification &lt;code&gt;b&lt;/code&gt; is coming. Additionally, it will trigger the new Observable &lt;code&gt;i2$&lt;/code&gt; and subscribes to that one.&lt;br&gt;
We already said, that &lt;code&gt;switchMap&lt;/code&gt; automatically subscribes to the inner &lt;code&gt;Observable&lt;/code&gt;. Additionally, it automatically unsubscribes from it, as soon as there is a new value coming from the source &lt;code&gt;Observable&lt;/code&gt;. This means that &lt;code&gt;switchMap&lt;/code&gt; also includes a build-in subscription-management mechanism.&lt;/p&gt;

&lt;p&gt;You can experience this operator in the linked Stackblitz example. It will trigger an HTTP call after you clicked somewhere. If you click fast enough you will notice that some of the HTTP calls are canceled. You can see that in the network view of the Chrome DevTools. If one of the requests is labeled with &lt;code&gt;canceled&lt;/code&gt;, this means, that the Observable, that executed the HTTP call, is unsubscribed from.&lt;/p&gt;

&lt;p&gt;Now that we know about &lt;code&gt;switchMap&lt;/code&gt;, I'd suggest, let's take a look at the other operators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing to similars
&lt;/h2&gt;

&lt;p&gt;So now it's time to keep my promise. I already mentioned &lt;code&gt;switchMap&lt;/code&gt; is very similar to &lt;code&gt;concatMap&lt;/code&gt;, &lt;code&gt;mergeMap&lt;/code&gt; &amp;amp; &lt;code&gt;exhaustMap&lt;/code&gt;. So what's the difference?&lt;/p&gt;

&lt;h3&gt;
  
  
  ConcatMap
&lt;/h3&gt;

&lt;p&gt;Let's start with &lt;a href="https://rxjs.dev/api/operators/concatMap" rel="noopener noreferrer"&gt;concatMap&lt;/a&gt;. &lt;code&gt;ConcatMap&lt;/code&gt; also passes notifications of a source observable to an inner observable. It subscribes to that and waits till it completes before it uses the next notification emitted by the source &lt;code&gt;observable&lt;/code&gt;. So potentially one could run into memory leaks if the source observable emits endlessly and faster than the inner observable can complete. &lt;code&gt;ConcatMap&lt;/code&gt; taking care of the order the inner observables were instantiated. Therefore it is the safest operator of those we cover in this article, from a business perspective. Theoretically you could run into the technical problem of having memory leaks, but still, if you are unsure which operator to choose, I'd recommend taking &lt;code&gt;concatMap&lt;/code&gt;. You'll be fine, more or less. So if you are performing HTTP requests inside the &lt;code&gt;concatMap&lt;/code&gt; function, you can be sure that one receives the response of the responses before the next notification of the source observable is passed to the inner observable. In the meantime, it buffers those notifications to be ready as soon as the inner observable completes.&lt;/p&gt;

&lt;p&gt;Maybe the following marble diagram sums up the behavior of &lt;code&gt;concatMap&lt;/code&gt; quite well. Instead of interrupting &lt;code&gt;i1$&lt;/code&gt; as it used to with &lt;code&gt;switchMap&lt;/code&gt;, it waits for the completion of &lt;code&gt;i1$&lt;/code&gt; and buffers the notifications in between, like &lt;code&gt;b&lt;/code&gt;. After the first stream completes it will start working on the buffered notifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58e1qsejjggrmmhrv1ln.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58e1qsejjggrmmhrv1ln.jpg" alt="concatMap marble diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also explore the behavior of &lt;code&gt;concatMap&lt;/code&gt; in the Stackblitz I linked above. You need to copy everything from the &lt;code&gt;operators/concatMap.ts&lt;/code&gt; file over to &lt;code&gt;index.ts&lt;/code&gt;. The code is pretty much the same as before, just instead that it is now using &lt;code&gt;concatMap&lt;/code&gt;. What you will notice if you click furiously somewhere in the window, is that it will make HTTP calls for each click you made, but just one by one. And even more important, it waits for every HTTP call to complete before instantiating the next one.&lt;/p&gt;

&lt;h3&gt;
  
  
  MergeMap
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://rxjs.dev/api/operators/mergeMap" rel="noopener noreferrer"&gt;MergeMap&lt;/a&gt; or &lt;code&gt;flatMap&lt;/code&gt; (which is just an alias of &lt;code&gt;mergeMap&lt;/code&gt;) is quite similar to &lt;code&gt;concatMap&lt;/code&gt;, although it doesn't consider the order and it doesn't wait for the completion of one inner observable before subscribing to the next one. If we stick to the HTTP example, theoretically one could run into the situation where multiple HTTP requests are initiated inside the mergeMap function, if it doesn't wait for its completion, one could receive the response of the 2nd instantiated before the first one emits its response.&lt;/p&gt;

&lt;p&gt;The following diagram displays the behavior quite well. Notifications of stream &lt;code&gt;i2$&lt;/code&gt; can be passed to the &lt;code&gt;output$&lt;/code&gt; stream, even though &lt;code&gt;i1$&lt;/code&gt; didn't completed yet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v9zji7gitc7gpncpmmc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v9zji7gitc7gpncpmmc.jpg" alt="mergeMap marble diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again you can try out the behavior of &lt;code&gt;mergeMap&lt;/code&gt; in the linked Stackblitz.&lt;/p&gt;

&lt;p&gt;Do note, that &lt;code&gt;mergeMap&lt;/code&gt; is definitely the wrong operator if the order of the notifications is important for you. If you need to process notifications sequentially, use &lt;code&gt;concatMap&lt;/code&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  ExhaustMap
&lt;/h3&gt;

&lt;p&gt;Last but not least, &lt;a href="https://rxjs.dev/api/operators/exhaustMap" rel="noopener noreferrer"&gt;exhaustMap&lt;/a&gt;. which is quite the opposite of &lt;code&gt;switchMap&lt;/code&gt;. While switchMap is unsubscribing from the inner observable as soon as there is an incoming notification from the source observable, exhaustMap ignores those notifications completely, until the inner observable completes. This operator is the perfect match if you fear nervously clicking users crashing your application. But be aware that notifications in between aren't buffered, they are completely ignored. This behavior makes it different from &lt;code&gt;concatMap&lt;/code&gt;, which will buffer those notifications.&lt;/p&gt;

&lt;p&gt;The following marble diagram demonstrates this very well. Notification 'b' is completely ignored! It waits until &lt;code&gt;i1$&lt;/code&gt; completes. Afterwards, it will be triggered by the next notification from the source observable, which is &lt;code&gt;c&lt;/code&gt; in the marble diagram below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqkx6sc86cxdylmb9uzq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqkx6sc86cxdylmb9uzq.jpg" alt="exhaustMap marble diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And again, you can play around with the behavior of &lt;code&gt;exhaustMap&lt;/code&gt; in the linked Stackblitz. If you will do things like double and triple clicks, you will notice, that those are ignored as long as the HTTP request is ongoing. Afterwards, you can click again to retrigger an HTTP call.&lt;/p&gt;

&lt;h2&gt;
  
  
  The risks of using switchMap
&lt;/h2&gt;

&lt;p&gt;Now that we know about all those fantastic operators, many of you might already have heard, that there might be some issues using &lt;code&gt;switchMap&lt;/code&gt;. Let's figure out what this rumor is all about.&lt;/p&gt;

&lt;p&gt;The problem with &lt;code&gt;switchMap&lt;/code&gt; is that even though it cancels HTTP requests from a client perspective, the backend anyway "receives" those requests and may perform some actions. The problem with that is, that the backend anyway handles the requests and might perform mutation on the objects. This would result in having a different state on the client side than in the backend. For example, you are performing an account transaction in combination with &lt;code&gt;switchMap&lt;/code&gt;. If you are initiating this multiple times, for the client it looks like it just happened once, but the backend receives multiple requests which leads to multiple transactions. And for sure no one wants to transfer money multiple times :D As long as you don't perform any mutation on the object instance on the server side or trigger any additional actions, you are good to go with &lt;code&gt;switchMap&lt;/code&gt;. Fetching an object or polling objects is a totally valid use case for &lt;code&gt;switchMap&lt;/code&gt;, but be aware of server-side actions or mutations!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;switchMap subscribes to the inner Observable with every new notification&lt;/li&gt;
&lt;li&gt;it automatically unsubscribes from an old inner Observable&lt;/li&gt;
&lt;li&gt;Be cautious when using switchMap&lt;/li&gt;
&lt;li&gt;Use concatMap when you are unsure

&lt;ul&gt;
&lt;li&gt;consider memory leaks of long-living Observables&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;use switchMap for fetching/polling/ read-only transactions&lt;/li&gt;

&lt;li&gt;be aware of server-side mutations or actions performed&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Special Thanks
&lt;/h2&gt;

&lt;p&gt;Many thanks to everyone, who helped me writing this blog article.&lt;br&gt;
Thanks to &lt;a href="https://twitter.com/toddmotto" rel="noopener noreferrer"&gt;Todd Motto&lt;/a&gt;, &lt;a href="https://twitter.com/wesgrimes" rel="noopener noreferrer"&gt;Wes Grimes&lt;/a&gt; and &lt;a href="https://twitter.com/BTroncone" rel="noopener noreferrer"&gt;Brian Troncone&lt;/a&gt; for reviewing the article and providing very valuable feedback. Additionally, I want to thank &lt;a href="https://twitter.com/Michael_Hladky" rel="noopener noreferrer"&gt;Michael Hladky&lt;/a&gt; for providing those marvelous marble diagrams!&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
