<?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: Deborah Kurata</title>
    <description>The latest articles on DEV Community by Deborah Kurata (@deborahk).</description>
    <link>https://dev.to/deborahk</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%2F212634%2Ff047170f-c0c6-4302-865e-00e5d4207ab4.jpeg</url>
      <title>DEV Community: Deborah Kurata</title>
      <link>https://dev.to/deborahk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deborahk"/>
    <language>en</language>
    <item>
      <title>RxJS Operator: switchMap</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Aug 2021 21:14:27 +0000</pubDate>
      <link>https://dev.to/deborahk/rxjs-operator-switchmap-17d</link>
      <guid>https://dev.to/deborahk/rxjs-operator-switchmap-17d</guid>
      <description>&lt;p&gt;The &lt;code&gt;switchMap&lt;/code&gt; RxJS Operator is a higher-order mapping operator that processes an emitted value but if another value is emitted, it will cancel that processing and switch over to the new value.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;categorySelected$&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;catId&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?cat=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;catId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;In the above code, the &lt;code&gt;switchMap&lt;/code&gt; will perform the HTTP get for the selected category. If a different category is selected, it will cancel that HTTP get request and perform an HTTP get for the newly selected category.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does switchMap Work?
&lt;/h2&gt;

&lt;p&gt;We can think of the switchMap like a coach that can't decide which runner should run in a race. But ultimately, only their last choice will run.&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%2Frzplmovdcwvvjq1o10dz.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzplmovdcwvvjq1o10dz.png" alt="image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's look at an Example
&lt;/h2&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%2Fw1w00ztcaovmlugc1tj5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1w00ztcaovmlugc1tj5.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the user clicks the category with an id of 2, changes their mind and picks 3, then decides they really want category 1.&lt;/p&gt;

&lt;p&gt;Following the numbers on the screen shot above: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The switchMap issues a get request for category 2,
&lt;/li&gt;
&lt;li&gt;then cancels that request and issues a get request for category 3, &lt;/li&gt;
&lt;li&gt;then cancels that request and issues a get request for category 1.&lt;/li&gt;
&lt;li&gt;Only the result for category 1 is emitted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this specific example, where the user can change their mind on the selected category, using switchMap is often the best choice. It will immediately react to the user's selection.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should We Use switchMap?
&lt;/h2&gt;

&lt;p&gt;The switchMap operator is great for responding to user actions, such as this example of the user selecting a product category. It can then switch as the user changes their mind.&lt;/p&gt;

&lt;p&gt;Also great for typeahead functionality.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>RxJS Operator: mergeMap</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Aug 2021 21:08:56 +0000</pubDate>
      <link>https://dev.to/deborahk/rxjs-operator-mergemap-1493</link>
      <guid>https://dev.to/deborahk/rxjs-operator-mergemap-1493</guid>
      <description>&lt;p&gt;The &lt;code&gt;mergeMap&lt;/code&gt; RxJS Operator is a higher-order mapping operator that processes each emitted value as it is emitted and returns the results as soon as they are available, which may not be in the order of the requests.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;categorySelected$&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;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;catId&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?cat=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;catId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;In the above code, the &lt;code&gt;mergeMap&lt;/code&gt; will perform the HTTP get as soon as an Id is emitted from &lt;code&gt;this.categorySelected$&lt;/code&gt;. When another Id is emitted, that request is performed concurrently.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does mergeMap Work?
&lt;/h2&gt;

&lt;p&gt;We can think of the mergeMap like the 800 m race: the runners all start at the same time. And they will finish the race as quickly as they can. The order of the finish won't necessarily be the order they started.&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%2Fdi8bom289duq8zkq9vaz.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdi8bom289duq8zkq9vaz.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's look at an Example
&lt;/h2&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%2Fi641wxr4r9t9s8r2jf23.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi641wxr4r9t9s8r2jf23.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the user clicks the category with an id of 2, changes their mind and picks 3, then decides they really want category 1.&lt;/p&gt;

&lt;p&gt;Following the numbers on the screen shot above: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The mergeMap issues a get request for category 2, &lt;/li&gt;
&lt;li&gt;when emitted, it issues a get request for category 3, &lt;/li&gt;
&lt;li&gt;when emitted, it then issues a get request for category 1. &lt;/li&gt;
&lt;li&gt;The result is emitted as soon as the response is returned.&lt;/li&gt;
&lt;li&gt;In the above example, the response for category 3 returns first,&lt;/li&gt;
&lt;li&gt;the response for category 1 returns next,&lt;/li&gt;
&lt;li&gt;and lastly the response for category 2 returns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this specific example, where the user can change their mind on the selected category, using mergeMap doesn't make sense. As shown in the above example, the last returned result may not be related to the most recent selection. (The user picked 1 last, but they now see the result for category 2.)&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should We Use mergeMap?
&lt;/h2&gt;

&lt;p&gt;The mergeMap is great for related data. Say we had a set of customer Ids and we wanted to get the detail for all of them, in no particular order. Using a mergeMap will get the data for all of them concurrently, providing a more performant user experience than getting them sequentially, one at a time, as with concatMap.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>RxJS Operator: concatMap</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Aug 2021 20:58:41 +0000</pubDate>
      <link>https://dev.to/deborahk/rxjs-operator-concatmap-3bno</link>
      <guid>https://dev.to/deborahk/rxjs-operator-concatmap-3bno</guid>
      <description>&lt;p&gt;The &lt;code&gt;concatMap&lt;/code&gt; RxJS Operator is a higher-order mapping operator that processes each emitted value in order and waits for its inner Observable to complete before processing the next item.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;categorySelected$&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;concatMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;catId&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?cat=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;catId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;In the above code, the &lt;code&gt;concatMap&lt;/code&gt; will perform the HTTP get and wait for the response before processing the next item emitted from &lt;code&gt;this.categorySelected$&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does concatMap Work?
&lt;/h2&gt;

&lt;p&gt;We can think of the concatMap like a relay race: the next runner can't start until the prior runner completes. And the runners cannot run out of sequence.&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%2F3mphi7bsop65f2x362wx.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mphi7bsop65f2x362wx.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's look at an Example
&lt;/h2&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%2F6armjetvmriql1953hv1.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6armjetvmriql1953hv1.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
In this example, the user clicks the category with an id of 2, changes their mind and picks 3, then decides they really want category 1.&lt;/p&gt;

&lt;p&gt;Following the numbers on the screen shot above: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The concatMap issues a get request for category 2, &lt;/li&gt;
&lt;li&gt;waits for the response, &lt;/li&gt;
&lt;li&gt;then issues a get request for category 3, &lt;/li&gt;
&lt;li&gt;waits for the response, &lt;/li&gt;
&lt;li&gt;then issues a get request for category 1, &lt;/li&gt;
&lt;li&gt;and waits for the response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this specific example, where the user can change their mind on the selected category, using concatMap doesn't make sense. It takes too long for each selection to be processed and returns results that are no longer needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should We Use concatMap?
&lt;/h2&gt;

&lt;p&gt;The concatMap is great for things like updates or deletes where you want to ensure that each request completes before the next one begins.&lt;/p&gt;

&lt;p&gt;It's also great any time that the request/response order matters.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Higher-Order Mapping Operators</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Aug 2021 20:47:02 +0000</pubDate>
      <link>https://dev.to/deborahk/higher-order-mapping-operators-1klm</link>
      <guid>https://dev.to/deborahk/higher-order-mapping-operators-1klm</guid>
      <description>&lt;p&gt;Higher-order mapping operators are used to map inner Observables. They include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;concatMap&lt;/li&gt;
&lt;li&gt;mergeMap&lt;/li&gt;
&lt;li&gt;switchMap&lt;/li&gt;
&lt;/ul&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%2Fx4x8j3yn0hy7ggaz2vwl.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4x8j3yn0hy7ggaz2vwl.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each higher-order mapping operator:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically subscribes to the inner Observable&lt;/li&gt;
&lt;li&gt;Flattens the resulting Observable, returning &lt;code&gt;Observable&amp;lt;T&amp;gt;&lt;/code&gt; instead of &lt;code&gt;Observable&amp;lt;Observable&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Automatically unsubscribe from the inner Observable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;categorySelected$&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;catId&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?cat=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;catId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;In this code, every time the user selects a new category, &lt;code&gt;this.categorySelected$&lt;/code&gt; emits the id of the selected category. That id is piped through a higher-order mapping operator (&lt;code&gt;switchMap&lt;/code&gt; in this case). The &lt;code&gt;switchMap&lt;/code&gt; automatically subscribes to the inner Observable, flattens the result (emitting an array of type &lt;code&gt;Product[]&lt;/code&gt; instead of &lt;code&gt;Observable&amp;lt;Product[]&amp;gt;&lt;/code&gt;), and unsubscribes from the inner Observable.&lt;/p&gt;

&lt;p&gt;Additional posts in this series look more closely at several higher-order mapping operators and when to use which.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Inner Observables and Higher Order Mapping</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Aug 2021 20:00:23 +0000</pubDate>
      <link>https://dev.to/deborahk/inner-observables-and-higher-order-mapping-hhe</link>
      <guid>https://dev.to/deborahk/inner-observables-and-higher-order-mapping-hhe</guid>
      <description>&lt;p&gt;When first starting out with RxJS, I didn't know what an "inner Observable" was, which made it difficult to fully understand the RxJS documentation. So, what is an inner Observable?&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%2Fe9o6xe6plnlj07k6lull.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe9o6xe6plnlj07k6lull.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When working with Observables, the Observable we are acting on is the outer Observable. The &lt;code&gt;this.categorySelected$&lt;/code&gt; Observable in the above example is an "outer Observable". We can pipe its emitted items through a pipeline of operations using the &lt;code&gt;pipe&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;If any of those operations return an Observable, that returned Observable is the "inner Observable". The http get request in the above example is an "inner Observable".&lt;/p&gt;

&lt;p&gt;In our code, we subscribe to the declared Observable, &lt;code&gt;products$&lt;/code&gt; in this example, either with an explicit subscribe or with the async pipe in our template. Subscribing to the declared Observable automatically subscribes to the outer Observable, which is &lt;code&gt;this.categorySelected$&lt;/code&gt; above. &lt;/p&gt;

&lt;p&gt;But how do we subscribe to the inner Observable? How do we unsubscribe?&lt;/p&gt;

&lt;p&gt;One option is to do something 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="c1"&gt;// Anti-Pattern!&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;sub&lt;/span&gt; &lt;span class="o"&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;categorySelected$&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="nx"&gt;catId&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?cat=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;catId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;span class="nx"&gt;result&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&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 the above code we explicitly subscribe to the outer Observable. Within the next function passed into the subscribe, we subscribe to the inner Observable. We can use &lt;code&gt;this.sub&lt;/code&gt; to unsubscribe from the outer Observable, but using the above code, we have no way to unsubscribe from the inner Observable.&lt;/p&gt;

&lt;p&gt;This type of code, with nested subscribes, is not recommended. It is often defined as an "anti-pattern", that is a pattern of code that is &lt;em&gt;not&lt;/em&gt; recommended and should be refactored.&lt;/p&gt;

&lt;p&gt;So how do you subscribe to those inner Observables? That's the purpose of the RxJS higher-order mapping operators. We'll cover three of the common higher-order mapping operators in upcoming posts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;concatMap&lt;/li&gt;
&lt;li&gt;mergeMap&lt;/li&gt;
&lt;li&gt;switchMap&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the best!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>Links to My Content</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Thu, 19 Nov 2020 22:18:29 +0000</pubDate>
      <link>https://dev.to/angular/links-to-my-content-2med</link>
      <guid>https://dev.to/angular/links-to-my-content-2med</guid>
      <description>&lt;p&gt;I'm often asked for links to specific topics, and thought it useful to centralize the list in one location.&lt;/p&gt;

&lt;p&gt;Here are a set of links to my talks, podcasts, and other external content, organized by topic. Links to my Pluralsight courses are listed in a separate section at the end.&lt;/p&gt;

&lt;h1&gt;
  
  
  RxJS in Angular
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://bit.ly/rxjs-youtube" rel="noopener noreferrer"&gt;YouTube Playlist&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Angular Community Meetup: "Reactive Programming with RxJS: Terms, Tips, &amp;amp; Techniques" (April 19, 2022)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.meetup.com/AngularCommunity/events/scvjtsydcgbzb/" rel="noopener noreferrer"&gt;event&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=2zRS6rZXpi4" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1BTDdGuOB_uzySsAT0uSwMoAA6Sciy03DI33RNVOcXkk/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackblitz.com/edit/angular-reactive-programming-deborahk" rel="noopener noreferrer"&gt;Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ng-Poland "RxJS in Angular: Terms, Tips, and Techniques" (January 2022)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ng-poland.pl/" rel="noopener noreferrer"&gt;event&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=""&gt;YouTube video - not available yet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1-57iZCC7zN-HiWxkjjeTzV187C-HsfOw5rGK8e8mgrQ/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/toh" rel="noopener noreferrer"&gt;Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enterprise NG "Designing Reactive Angular Components (RxJS)" (December 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=""&gt;YouTube video - not available yet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1vTPeZ13X5iSxwCS78-oSNedg4jwGF2hX-_c01xY3hxU/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/Angular-ReactiveDevelopment" rel="noopener noreferrer"&gt;Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Angular World Tour (Sussex) "Reactive Programming with RxJS: Terms, Tips and Techniques" (November 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://youtu.be/AlKc28Jl1Ps?t=1390" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1SBvkTfepKg8c1yJ5NVd3EHrwyq49MXKvVXgI3qO__xg/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackblitz.com/edit/angular-reactive-programming-deborahk" rel="noopener noreferrer"&gt;Code (Stackblitz)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Angular World Tour (Memphis) "Reactive Programming with RxJS" (October 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1vOSOlEjhnUpfC7fmGBSU_VndJHNJieA7qfdb4h9HgJk/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackblitz.com/edit/angular-reactive-programming-deborahk" rel="noopener noreferrer"&gt;Code (Stackblitz)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Angular World Tour (Athens) "Reactive Programming with RxJS" (September 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=qxjt5vlG58s&amp;amp;list=PLErOmyzRKOCrtHaQk-qiq3YhhD_J7K9rA&amp;amp;index=1&amp;amp;t=1771s" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1N2-4uuS9QoNpxhXihrJKq2cK8fQxcxQVuSIWoM30XuE/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackblitz.com/edit/angular-reactive-programming-deborahk" rel="noopener noreferrer"&gt;Code (Stackblitz)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Angular World Tour (Kansas City) "RxJS Terms, Tips, and Techniques" (July 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=iSsch65n8Yw" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1w9pxAuH8Xtwa3cXLYu3IhsB4FCqbkeKTBOtYmZelsM0/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RxJS Live! Asia Conference "RxJS in Angular: Pipeline Patterns" (April 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=0EefbG6N3vY" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1SNb1BJuw0UNpRBepFST4bnw8k8EGYRgLjJfWWB3tMBM/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/Angular-Posts" rel="noopener noreferrer"&gt;Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ng-conf "4 Wicked RxJS Pipelines for the Real World" (April 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1NIPhplAzrHBhqZWZTYcGwAJRCQqbF5HiFqhL44FhGSo/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=SPy35fOjRyU&amp;amp;t=2s" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pluralsight Tech Skills Day 2021 (April 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://app.pluralsight.com/library/courses/techskills-day-2021-session-04" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Angular Utah Meetup "RxJS Terms and Patterns" (January 2021)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?t=869&amp;amp;v=7AvI6F8ifqU" rel="noopener noreferrer"&gt;YouTube  video&lt;/a&gt; (My talk is 45 minutes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ng-SriLanka "RxJS Terms, Tips, and Techniques" (December 2020)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=knUgpZrxUII" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1-8QFxwbd2ilEZI3uLFrqPzCXehGeaEGtNZ4dxaISrgE/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/toh" rel="noopener noreferrer"&gt;Sample code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enterprise NG "RxJS Patterns in Angular" (November 2020)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/presentation/d/1SXC3V0cHX0M79hkO2k4c9CdccJUMLGxJ9sepEYUNoM0/edit?usp=sharing" rel="noopener noreferrer"&gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/toh" rel="noopener noreferrer"&gt;Sample code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=uv_sblwIJag" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://app.pluralsight.com/course-player?clipId=ba4a5144-0eb7-4c65-8f39-26b89171292e" rel="noopener noreferrer"&gt;Video - requires Pluralsight membership&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DevReach 2.0 Day 4 Angular Relay (October 2020)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=aayCeLRhCUg&amp;amp;feature=youtu.be&amp;amp;t=12201" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/kui-tour-of-heroes" rel="noopener noreferrer"&gt;Sample code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://open.spotify.com/episode/6QItH9k192HgEySI8PWIPp" rel="noopener noreferrer"&gt;The Angular Show (Spotify)&lt;/a&gt; Episode #32 - "State Management pt. 4 - RxJS &amp;amp; Singleton Services" (September 2020)&lt;/p&gt;

&lt;p&gt;Angular International Women's Day "RxJS Action and Data Streams: A Love Story" (August 2020)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=OKZBHuYa-wc" rel="noopener noreferrer"&gt;YouTube video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/DeborahK/Angular-ActionStreams" rel="noopener noreferrer"&gt;Sample Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=ZdS9uOl4OJk" rel="noopener noreferrer"&gt;ng-India&lt;/a&gt; "RxJS in Angular: Action and Data Streams" (August 2020)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://webrush.io/episodes/episode-91-solving-common-rxjs-scenarios-in-angular-with-deborah-kurata" rel="noopener noreferrer"&gt;Real Talk JavaScript&lt;/a&gt; Episode #91 - "Solving Common RxJS Scenarios in Angular with Deborah Kurata" (July 2020)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=ldRdjc-60PM" rel="noopener noreferrer"&gt;ngconf: Hardwired&lt;/a&gt; "Why Should You Care About RxJS Higher-order Mapping Operators?" (June 2020)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=e2KAn50QBBw" rel="noopener noreferrer"&gt;ng Malasia&lt;/a&gt; "Collect, Combine, and Cache RxJS Streams for User-Friendly Results" (August 2019)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=Z76QlSpYcck" rel="noopener noreferrer"&gt;ngconf&lt;/a&gt; "Data Composition with RxJS" (May 2019)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.devshows.dev/podcasts/adventures-in-angular/aia-237-more-on-rxjs-with-deborah-kurata" rel="noopener noreferrer"&gt;Adventures in Angular&lt;/a&gt; Episode #237 - "More on RxJS with Deborah Kurata" (April 2019)&lt;/p&gt;

&lt;h1&gt;
  
  
  Other Angular Topics
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=LaIAHOSKHCQ" rel="noopener noreferrer"&gt;ngconf&lt;/a&gt; "N Things You Didn't Know About the Router" (April 2018)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=ntJ-P-Cvo7o" rel="noopener noreferrer"&gt;ngconf&lt;/a&gt; "Module vs Module" (April 2017)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=ihYc9y7dQA0" rel="noopener noreferrer"&gt;ngconf&lt;/a&gt; "It's Just a Textbox, What Could Go Wrong?: Angular 2 Forms and Validation" (May 2016)&lt;/p&gt;

&lt;h1&gt;
  
  
  Pluralsight Courses
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Angular Courses
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/angular-2-getting-started-update" rel="noopener noreferrer"&gt;Angular: Getting Started&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/angular-2-reactive-forms" rel="noopener noreferrer"&gt;Angular: Reactive Forms&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/angular-routing" rel="noopener noreferrer"&gt;Angular Routing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/rxjs-angular-reactive-development" rel="noopener noreferrer"&gt;RxJS in Angular: Reactive Development&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/angular-ngrx-getting-started" rel="noopener noreferrer"&gt;Angular NgRx: Getting Started&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/angular-component-communication" rel="noopener noreferrer"&gt;Angular Component Communication&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  C# Courses
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/object-oriented-programming-fundamentals-csharp" rel="noopener noreferrer"&gt;Object-Oriented Programming Fundamentals in C#&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/csharp-best-practices-improving-basics" rel="noopener noreferrer"&gt;C# Best Practices: Improving on the Basics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/csharp-best-practices-collections-generics" rel="noopener noreferrer"&gt;C# Best Practices: Collections and Generics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.pluralsight.com/library/courses/csharp-defensive-coding" rel="noopener noreferrer"&gt;Defensive Coding in C#&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Other
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://anchor.fm/tiaras-and-tech/episodes/Technical-Management-Lessons-Learned-with-Deborah-Kurata-e183969" rel="noopener noreferrer"&gt;Tiaras and Tech Podcast&lt;/a&gt; "Technical Management: Lessons Learned with Deborah Kurata" (November 2021)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=CSMcQ5972B4" rel="noopener noreferrer"&gt;All Hands on Tech&lt;/a&gt; "Andrew Yang and Deborah Kurata talk tech skills with Hank Green" (April 2020)&lt;/p&gt;

</description>
      <category>angular</category>
    </item>
    <item>
      <title>RxJS Tip: Creation Function: fromEvent()</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Tue, 17 Nov 2020 19:36:19 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-creation-function-fromevent-4mp0</link>
      <guid>https://dev.to/angular/rxjs-tip-creation-function-fromevent-4mp0</guid>
      <description>&lt;p&gt;The RxJS &lt;code&gt;fromEvent&lt;/code&gt; is a creation function that emits events.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Does It Work?
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;fromEvent&lt;/code&gt; creation function takes in an event target object and event type string, creates an Observable, and emits the specified events as they occur.&lt;/p&gt;

&lt;p&gt;It works with any document object model (DOM) element, such as buttons, input elements, and the document itself.&lt;/p&gt;

&lt;p&gt;Find the list of valid event types in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element#Events" rel="noopener noreferrer"&gt;Mozilla docs here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fju4hnw248rnl72yzk8of.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fju4hnw248rnl72yzk8of.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example watches for &lt;code&gt;click&lt;/code&gt; events on the &lt;code&gt;myInput&lt;/code&gt; element. &lt;/p&gt;

&lt;p&gt;Each &lt;code&gt;click&lt;/code&gt; event is emitted and logged to the console.&lt;/p&gt;

&lt;h1&gt;
  
  
  How About Some Examples?
&lt;/h1&gt;

&lt;p&gt;This example watches for &lt;code&gt;click&lt;/code&gt; events anywhere on the document (web page). When a click event is emitted, it's x and y coordinates are logged.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Click event on the document page&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="err"&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="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;MouseEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&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="s2"&gt;`x: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, y: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;The following example first locates the element with an id of &lt;code&gt;name&lt;/code&gt;. It then watches for &lt;code&gt;keyup&lt;/code&gt; events in the &lt;code&gt;name&lt;/code&gt; element. Each &lt;code&gt;keyup&lt;/code&gt; event is emitted and it's key code is logged to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Keyup event on an input element&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyup&lt;/span&gt;&lt;span class="dl"&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;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;KeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&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="s2"&gt;`key: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;Here is a more complete Angular example. In the template, it uses a template reference variable (filterInput) on an input element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Template&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt; &lt;span class="na"&gt;#filterInput&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the component, it uses the &lt;code&gt;ViewChild&lt;/code&gt; decorator to locate the element using the template reference variable (filterInput). In the &lt;code&gt;ngAfterViewInit&lt;/code&gt; lifecycle hook method, it uses the &lt;code&gt;fromEvent&lt;/code&gt; creation function, passing in the found element and the desired event (keyup).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;br&gt;
&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;import&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ViewChild&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ElementRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;AfterViewInit&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="err"&gt; &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="k"&gt;import&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;fromEvent&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs&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="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./product-list.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ProductListComponent&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;implements&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;AfterViewInit&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ViewChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;filterInput&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;filterElementRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ElementRef&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="nf"&gt;ngAfterViewInit&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&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;filterElementRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nativeElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyup&lt;/span&gt;&lt;span class="dl"&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;span class="err"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;KeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&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="s2"&gt;`key: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&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;blockquote&gt;
&lt;p&gt;Note that there are often more "Angular" ways to accomplish event handling using event binding. Consider using event binding as a first choice and only use &lt;code&gt;fromEvent&lt;/code&gt; if event binding doesn't provide the features you require.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  What Are Some Common Uses?
&lt;/h1&gt;

&lt;p&gt;Here are some common uses of the &lt;code&gt;fromEvent&lt;/code&gt; creation function.&lt;/p&gt;

&lt;p&gt;Tracking and responding to click events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tracking and responding to keyboard events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tracking and responding to clipboard events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&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;paste&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Where's the Code?
&lt;/h1&gt;

&lt;p&gt;Here is a &lt;a href="https://stackblitz.com/edit/rxjs-fromevent-deborahk" rel="noopener noreferrer"&gt;link to sample code&lt;/a&gt; demonstrating the &lt;code&gt;fromEvent&lt;/code&gt; creation function. Feel free to fork it and try it out.&lt;/p&gt;

&lt;h1&gt;
  
  
  What About the Marble Diagram?
&lt;/h1&gt;

&lt;p&gt;This is the &lt;code&gt;fromEvent&lt;/code&gt; creation function shown on a marble diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs65p90dnrhnmidmgdpl1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs65p90dnrhnmidmgdpl1.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
Thanks to &lt;a class="mentioned-user" href="https://dev.to/michael_hladky"&gt;@michael_hladky&lt;/a&gt; for this marble diagram.&lt;/p&gt;

&lt;p&gt;Do you have another common use of &lt;code&gt;fromEvent&lt;/code&gt;? If so, please leave a comment.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>RxJS Tip: Creation Function: from()</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Mon, 16 Nov 2020 23:37:39 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-creation-function-from-59cj</link>
      <guid>https://dev.to/angular/rxjs-tip-creation-function-from-59cj</guid>
      <description>&lt;p&gt;The RxJS &lt;code&gt;from&lt;/code&gt; function is a creation function that iterates an object and emits its values.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Does It Work?
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;from&lt;/code&gt; creation function can convert many kinds of things to an Observable. It can take in an array, a promise, or other iterable object. It creates an Observable, iterates the provided object emitting its values, and completes.&lt;/p&gt;

&lt;p&gt;The Observable created with &lt;code&gt;from&lt;/code&gt; is generally synchronous, meaning the values are iterated and emitted, and the Observable completes immediately after it is subscribed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5jfcep858d86t3he66n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5jfcep858d86t3he66n.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example passes an array of three strings to the &lt;code&gt;from&lt;/code&gt; function and subscribes.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;from&lt;/code&gt; iterates the array, emitting each of the three array elements which are logged.&lt;/p&gt;

&lt;h1&gt;
  
  
  How About Some Examples?
&lt;/h1&gt;

&lt;p&gt;This example iterates an array, emitting each of its elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Array: Iterates and emits the elements&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&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="nx"&gt;x&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 42,72,21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following example iterates the characters of a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Iterates&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;emits&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Apple1&lt;/span&gt;&lt;span class="dl"&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;span class="nx"&gt;x&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// A,p,p,l,e,1 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example emits the result of a promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Promise: Emits the fulfilled result of the promise&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&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="nx"&gt;x&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Apple1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What Are Some Common Uses?
&lt;/h1&gt;

&lt;p&gt;In addition to converting other structures to Observables, here are some common uses of the &lt;code&gt;from&lt;/code&gt; creation function.&lt;/p&gt;

&lt;p&gt;Sample code to try out a pipeline of operations when working with an array.&lt;br&gt;
&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;from&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User selects items for processing (ids of products in a cart, ids of selected songs for a playlist, etc.), managed as an array.&lt;br&gt;
&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;from&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Where's the Code?
&lt;/h1&gt;

&lt;p&gt;Here is a &lt;a href="https://stackblitz.com/edit/rxjs-from-deborahk" rel="noopener noreferrer"&gt;link to sample code&lt;/a&gt; demonstrating the &lt;code&gt;from&lt;/code&gt; creation function. Feel free to fork it and try it out.&lt;/p&gt;

&lt;h1&gt;
  
  
  What About the Marble Diagram?
&lt;/h1&gt;

&lt;p&gt;This is the &lt;code&gt;from&lt;/code&gt; creation function shown on a marble diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6gnp8qnrgabdbu8trn32.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6gnp8qnrgabdbu8trn32.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a class="mentioned-user" href="https://dev.to/michael_hladky"&gt;@michael_hladky&lt;/a&gt; for this marble diagram.&lt;/p&gt;

&lt;p&gt;Do you have another common use of &lt;code&gt;from&lt;/code&gt;? If so, please leave a comment.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>RxJS Tip: Creation Function: of()</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Mon, 16 Nov 2020 23:08:07 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-creation-function-of-1j1p</link>
      <guid>https://dev.to/angular/rxjs-tip-creation-function-of-1j1p</guid>
      <description>&lt;p&gt;The RxJS &lt;code&gt;of&lt;/code&gt; function is a creation function that emits specified values.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Does It Work?
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;of&lt;/code&gt; creation function takes in a set of values, creates an Observable that emits those values, and completes.&lt;/p&gt;

&lt;p&gt;The Observable created with &lt;code&gt;of&lt;/code&gt; is synchronous, meaning the values are emitted and it completes immediately after it is subscribed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi25bunt092y1ji82rxnb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi25bunt092y1ji82rxnb.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example passes three strings to the &lt;code&gt;of&lt;/code&gt; function and subscribes.&lt;/p&gt;

&lt;p&gt;Each of the three strings are then emitted and logged.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Are Some Common Uses?
&lt;/h1&gt;

&lt;p&gt;Sample code to try out a pipeline of operations.&lt;br&gt;
&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User selects items for processing (ids of products in a cart, ids of selected songs for a playlist, etc.) and those items are emitted from an Observable.&lt;br&gt;
&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="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For branching when one branch returns an Observable and another returns a static value as an Observable (such as null, an empty array, or an initialized value).&lt;br&gt;
&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="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&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;initializedProduct&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a more concrete example of branching:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="nf"&gt;getProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;      &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&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;initializeProduct&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- of() used here&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&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;productsUrl&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="err"&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&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="err"&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;data&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="err"&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&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;handleError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;      &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&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 ensures that each branch returns an Observable.&lt;/p&gt;

&lt;p&gt;NOTE: This example is meant to show usage of of() and is NOT meant to show the best technique to accomplish this objective.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where's the Code?
&lt;/h1&gt;

&lt;p&gt;Here is a &lt;a href="https://stackblitz.com/edit/rxjs-of-deborahk" rel="noopener noreferrer"&gt;link to sample code&lt;/a&gt; demonstrating the &lt;code&gt;of&lt;/code&gt; creation function. Feel free to fork it and try it out.&lt;/p&gt;

&lt;h1&gt;
  
  
  What About the Marble Diagram?
&lt;/h1&gt;

&lt;p&gt;This is the &lt;code&gt;of&lt;/code&gt; creation function shown on a marble diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx3p0veau0kmiex63vi2d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx3p0veau0kmiex63vi2d.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
Thanks to &lt;a class="mentioned-user" href="https://dev.to/michael_hladky"&gt;@michael_hladky&lt;/a&gt; for this marble diagram.&lt;/p&gt;

&lt;p&gt;Do you have another common use of &lt;code&gt;of&lt;/code&gt;? If so, please leave a comment.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>RxJS Tip: Use a Creation Function</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Mon, 16 Nov 2020 22:34:22 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-use-a-creation-function-1523</link>
      <guid>https://dev.to/angular/rxjs-tip-use-a-creation-function-1523</guid>
      <description>&lt;p&gt;Some of the Angular features, such as Http, automatically create and return Observables. But in some cases, you may want to create your own Observables.&lt;/p&gt;

&lt;p&gt;Create your own Observable using a &lt;strong&gt;creation function&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Is a Creation Function?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;creation function&lt;/strong&gt; is an RxJS library function that takes in arguments and returns an Observable.&lt;/p&gt;

&lt;p&gt;You can create an Observable from a set, an array, a timer, keyboard events, mouse events, and much more.&lt;/p&gt;

&lt;p&gt;This series covers the more common creation functions.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>RxJS Tip: Understand the Terminology: Observer</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Mon, 16 Nov 2020 21:41:59 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-understand-the-terminology-observer-1ca8</link>
      <guid>https://dev.to/angular/rxjs-tip-understand-the-terminology-observer-1ca8</guid>
      <description>&lt;p&gt;We've discussed &lt;a href="https://dev.to/angular/rxjs-tip-understand-the-terminology-observable-52ph"&gt;Observable&lt;/a&gt; and &lt;a href="https://dev.to/angular/rxjs-tip-understand-the-terminology-subscription-4j03"&gt;Subscription&lt;/a&gt;. Another key RxJS concept is &lt;strong&gt;Observer&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Is an Observer?
&lt;/h1&gt;

&lt;p&gt;An Observer watches for emissions and notifications from an Observable after a consumer subscribes to that Observable.&lt;/p&gt;

&lt;p&gt;An Observer defines an interface with callback functions for each type of Observable notification: next, error, and complete.&lt;/p&gt;

&lt;p&gt;Use the &lt;strong&gt;next&lt;/strong&gt; callback to process the emitted item.&lt;br&gt;
Use the &lt;strong&gt;error&lt;/strong&gt; callback to implement exception handling.&lt;br&gt;
Use the &lt;strong&gt;complete&lt;/strong&gt; callback to perform cleanup when the Observable is complete. (This is not used often in an Angular application.)&lt;/p&gt;
&lt;h1&gt;
  
  
  How Do You Define an Observer?
&lt;/h1&gt;

&lt;p&gt;There are several ways to define an Observer.&lt;/p&gt;
&lt;h3&gt;
  
  
  Explicitly (Uncommon)
&lt;/h3&gt;

&lt;p&gt;Though not common, you can define an Observer explicitly by creating an object with three callback functions: next, error, and complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Define an explicit observer (uncommon)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;apple&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="s2"&gt;`Apple was emitted &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&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="s2"&gt;`Error occurred: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;:&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="s2"&gt;`No more apples, go home`&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;The &lt;strong&gt;next&lt;/strong&gt; method argument is the emitted item. The arrow function specifies what to do with that item. In this case, we are simply logging it to the console.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;error&lt;/strong&gt; method argument is the error emitted when an error occurs. The arrow function specifies what to do with the error notification. In this case, we are logging the error to the console.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;complete&lt;/strong&gt; method has no argument. The arrow function defines what to do when the Observable is complete. In this case, it logs a message to the console.&lt;/p&gt;

&lt;p&gt;We then pass that Observer object into the Observable subscribe method to react to the Observable's emissions and notifications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Pass the Observer into the subscribe (uncommon)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pass a Single Callback
&lt;/h3&gt;

&lt;p&gt;It is more common to pass the Observer callback functions directly into the Observable subscribe method.&lt;/p&gt;

&lt;p&gt;You can only pass one object to the subscribe method.&lt;/p&gt;

&lt;p&gt;If you only need the next callback, pass it directly as the subscribe argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Pass the next callback function directly&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="nx"&gt;apple&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="s2"&gt;`Apple was emitted &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;h3&gt;
  
  
  Pass an Observer Object
&lt;/h3&gt;

&lt;p&gt;Since you can only pass one object to subscribe, if you need to handle multiple types of notifications, pass an Observer object with the desired set of callbacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Pass an Observer object with callback arrow functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;apple&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="s2"&gt;`Apple was emitted &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&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="s2"&gt;`Error occurred: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;:&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="s2"&gt;`No more apples, go home`&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;Notice that the above code passes an object into the subscribe method with next, error, and complete methods. You only need to specify the methods for the notifications you'll handle. So if you don't need to process the complete notification, you don't need to specify it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if You Don't Want to Use Arrow Functions?
&lt;/h3&gt;

&lt;p&gt;The prior examples all used arrow functions, denoted with &lt;code&gt;=&amp;gt;&lt;/code&gt;. Some developers may prefer to use declared named functions instead of arrow functions when defining Observer callbacks. Like this:&lt;br&gt;
&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;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`Apple was emitted &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`Error occurred: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
  &lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`No more apples, go home`&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;Notice the syntax difference. Here we define each function (next) with it's parameter (apple) and function body denoted with {}.&lt;/p&gt;

&lt;p&gt;But watch out for &lt;code&gt;this&lt;/code&gt;. In TypeScript (and in JavaScript), &lt;code&gt;this&lt;/code&gt; is scoped to the function. So if you have code like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Watch out for `this`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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;apple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;apple&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// Does NOT reference the &lt;/span&gt;
                                      &lt;span class="c1"&gt;// class-level variable&lt;/span&gt;
  &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`Error occurred: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
  &lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`No more apples, go home`&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;It may not work as expected. The &lt;code&gt;this.apple&lt;/code&gt; will &lt;em&gt;not&lt;/em&gt; reference a class-level variable and will instead define a function-scoped variable.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Do the Pieces Fit Together?
&lt;/h1&gt;

&lt;p&gt;The Observable, Observer, and Subscription work together to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tell the Observable to start emissions/notifications&lt;/li&gt;
&lt;li&gt;Provide callback functions to react to those emissions/notifications&lt;/li&gt;
&lt;li&gt;Set up a subscription that allows for unsubscribing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqper84x94v6gqpige6cv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqper84x94v6gqpige6cv.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the concepts shown on a more formal marble diagram.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fabne9lv8iggsfpumd55z.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fabne9lv8iggsfpumd55z.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a class="mentioned-user" href="https://dev.to/michael_hladky"&gt;@michael_hladky&lt;/a&gt; for this marble diagram.&lt;/p&gt;

&lt;p&gt;Here is a more common example usage in an Angular application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service&lt;/strong&gt;&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsUrl&lt;/span&gt;&lt;span class="p"&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;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
      &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&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;handleError&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 the above code, products$ represents the Observable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is a common practice to add a &lt;code&gt;$&lt;/code&gt; suffix to a variable that represents an Observable. This makes it easier to see when the code is working with an Observable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&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;sub&lt;/span&gt; &lt;span class="o"&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;productService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&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="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&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;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;err&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 the component, the Observer object is passed into the subscribe method, defining two callbacks: next and error.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;this.sub&lt;/code&gt; represents the Subscription returned from the subscribe method. This is used to unsubscribe on ngOnDestroy.&lt;/p&gt;

&lt;p&gt;I hope that clarified the meaning of the term &lt;strong&gt;Observer&lt;/strong&gt; and demonstrates how three key RxJS concepts: Observable, Subscription, and Observer work together.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>RxJS Tip: Understand the Terminology: Subscription</title>
      <dc:creator>Deborah Kurata</dc:creator>
      <pubDate>Mon, 16 Nov 2020 20:34:31 +0000</pubDate>
      <link>https://dev.to/angular/rxjs-tip-understand-the-terminology-subscription-4j03</link>
      <guid>https://dev.to/angular/rxjs-tip-understand-the-terminology-subscription-4j03</guid>
      <description>&lt;p&gt;A key concept when using RxJS is &lt;strong&gt;Subscription&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Is a Subscription?
&lt;/h1&gt;

&lt;p&gt;A Subscription is an object that represents execution of an Observable.&lt;/p&gt;

&lt;p&gt;Like the days of old when you couldn't get a newspaper delivered unless you subscribed, Observables don't do anything until a consumer subscribes.&lt;/p&gt;

&lt;p&gt;For example, if your component (the consumer) wants to be notified when an Observable emits the response returned from an Http call, the component subscribes to that Observable.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Do You Subscribe?
&lt;/h1&gt;

&lt;p&gt;One way to subscribe to an Observable is with its subscribe method.&lt;br&gt;
&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;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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;That method tells the Observable to start emitting AND creates and returns a Subscription.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's good practice to unsubscribe from any Observable you subscribe to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  How Do You Unsubscribe?
&lt;/h1&gt;

&lt;p&gt;Use the unsubscribe method of a Subscription to unsubscribe. The unsubscribe method lets the Observable know that the consumer no longer wants to receive emissions or notifications.&lt;br&gt;
&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;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source$&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="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's examine a more concrete example.&lt;/p&gt;

&lt;p&gt;Here is an Http request in a service:&lt;br&gt;
&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;products$&lt;/span&gt; &lt;span class="o"&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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsUrl&lt;/span&gt;&lt;span class="p"&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;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
      &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&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;handleError&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;Here is the subscription in a component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&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;sub&lt;/span&gt; &lt;span class="o"&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;productService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&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="nx"&gt;products&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&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;Here is the unsubscribe in the component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnDestroy&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&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;sub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&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;h1&gt;
  
  
  Once Subscribed, How Do You Capture the Emissions/Notifications?
&lt;/h1&gt;

&lt;p&gt;To perform an operation when an item is emitted from an Observable, an error occurs, or the Observable is complete, you need an &lt;a href="https://dev.to/deborahk/rxjs-tip-understand-the-terminology-observer-1ca8"&gt;&lt;strong&gt;Observer&lt;/strong&gt;&lt;/a&gt;, discussed in a later tip.&lt;/p&gt;

&lt;p&gt;I hope that's clarified the meaning of the term &lt;strong&gt;Subscription&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
    </item>
  </channel>
</rss>
