<?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: Omar E. Lopez</title>
    <description>The latest articles on DEV Community by Omar E. Lopez (@omenlog).</description>
    <link>https://dev.to/omenlog</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%2F52684%2F63ce8660-daa9-4c0a-a1f0-d663e7e36157.jpg</url>
      <title>DEV Community: Omar E. Lopez</title>
      <link>https://dev.to/omenlog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omenlog"/>
    <language>en</language>
    <item>
      <title>Introducing genix for build event driven applications</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Mon, 19 Oct 2020 20:34:27 +0000</pubDate>
      <link>https://dev.to/omenlog/introducing-genix-for-build-event-driven-applications-4gog</link>
      <guid>https://dev.to/omenlog/introducing-genix-for-build-event-driven-applications-4gog</guid>
      <description>&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@drew_beamer?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Drew Beamer&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/split?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/genix" rel="noopener noreferrer"&gt;genix&lt;/a&gt; is a new zero dependency library for build &lt;a href="https://martinfowler.com/articles/201701-event-driven.html" rel="noopener noreferrer"&gt;event driven&lt;/a&gt; applications, it make easier reach low levels of coupling in our applications and at the same time give us a set of tools that make the testing process very smoothly. The main building blocks in &lt;code&gt;genix&lt;/code&gt; are events and commands, this concepts are very similar being its main differences semantics. In this post I want present its the basic features, and then in future posts show how we can use it with more complete examples. Being said that let's start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;As I mentioned &lt;code&gt;genix&lt;/code&gt; allow us develop applications very easy to test, with low levels of coupling through the use of events. An event as usual represent something that happened or changed and they can be used as a notification mechanism that connect different components of our applications. Example of events can be &lt;strong&gt;orderReady&lt;/strong&gt;, &lt;strong&gt;userLogged&lt;/strong&gt;, &lt;strong&gt;paymentDone&lt;/strong&gt;, always they should communicate actions that have already ocurred.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;genix&lt;/code&gt; we can work with events using the following functions:&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;// register a handler for some event name&lt;/span&gt;
&lt;span class="nf"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// emit an event so every handler function &lt;/span&gt;
&lt;span class="c1"&gt;// associated to it will be executed&lt;/span&gt;
&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arguments&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 implement an example which will be a counter that increase a value every second and after 10 seconds the value is restored to zero, it's is a very simple problem but it serve to show events in action.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;genix&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;++&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;`Value updated &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&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;onEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10SecondsPassed&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Set Initial value &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10SecondsPassed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the previous snippet we can say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;counter&lt;/code&gt; and &lt;code&gt;ticker&lt;/code&gt; don't know anything about each other, they are completely independent being this a basic feature of pub/sub mechanisms.&lt;/li&gt;
&lt;li&gt;handlers should be registered before emit events, as you can see &lt;code&gt;counter&lt;/code&gt; function is executed before &lt;code&gt;ticker&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;privacy is reached through JS closure, this is something not obtained from &lt;code&gt;genix&lt;/code&gt; but I think is good highlight it.&lt;/li&gt;
&lt;li&gt;In this example wasn't used, but &lt;code&gt;onEvent&lt;/code&gt; return a subscription object with an &lt;code&gt;unsubscribe&lt;/code&gt; method that allow us cancel handlers in order to avoid memory leaks.&lt;/li&gt;
&lt;li&gt;The API of our components specifically &lt;code&gt;counter&lt;/code&gt; in this case are the events that they register.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Commands
&lt;/h2&gt;

&lt;p&gt;On the other hand in &lt;code&gt;genix&lt;/code&gt; we have commands. Commands are similar to events in the sense that a command has a name with a handler associated to it, but besides that they have important differences.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Semantically a command represent a future action, they are like an order that we want execute, so when we run a command we are triggering an action.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;We can have only one handler per command&lt;/strong&gt;, if we try to associate two handler to the same command we get an exception, so with events we can have more than one handler for the same event but this isn't the case with commands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When a command is executed it can return some value&lt;/strong&gt; , based that a command is an action that we are executing, we can get a value returned from it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Commands should be named with a verb in imperative mood, for example &lt;strong&gt;finishOrder&lt;/strong&gt;, &lt;strong&gt;loadData&lt;/strong&gt;, &lt;strong&gt;executePayment&lt;/strong&gt;. In the command API there are 2 functions, one to register commands and another to execute them:&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;// register a handler for some command name&lt;/span&gt;
&lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commandName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// execute a command passing arguments&lt;/span&gt;
&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commandName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see our example but using commands:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;genix&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&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="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resetValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;'&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;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resetValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;ticker&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 snippet we can note that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Again &lt;code&gt;counter&lt;/code&gt; and &lt;code&gt;ticker&lt;/code&gt; didn't know anything about each other which is very helpful in order to hide implementation details.&lt;/li&gt;
&lt;li&gt;In this case the public API of our &lt;code&gt;counter&lt;/code&gt; if the set of commands registered.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ticker&lt;/code&gt; in this example isn't notifying, instead it's like giving orders, the same behavior is obtained but with different semantic mechanism.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;After see events and commands in &lt;code&gt;genix&lt;/code&gt; and how they can be used to connect different components, now is time to speak about testing to show others features of this library.&lt;br&gt;
Testing examples will be around the &lt;code&gt;counter&lt;/code&gt; function, the implementation will be changed a little bit in order to mix events and commands so we get a more complete example that show many capabilities.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;genix&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getInitialValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&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="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;valueUpdated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resetValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resetValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 3 important changes in our &lt;code&gt;counter&lt;/code&gt; function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;counter&lt;/code&gt; register a new &lt;code&gt;getValue&lt;/code&gt; command, it will be used like a getter to expose our &lt;code&gt;value&lt;/code&gt; variable being that very helpful in our tests.&lt;/li&gt;
&lt;li&gt;It depends on &lt;code&gt;getInitialValue&lt;/code&gt; command to get the initial value that now isn't passed as argument, so to &lt;code&gt;counter&lt;/code&gt; work properly this command should be defined in some way.&lt;/li&gt;
&lt;li&gt;When the &lt;code&gt;tick&lt;/code&gt; event is emitted &lt;code&gt;counter&lt;/code&gt; update &lt;code&gt;value&lt;/code&gt; and emit a new &lt;code&gt;valueUpdated&lt;/code&gt; event passing the new value as argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's write a few tests for &lt;code&gt;counter&lt;/code&gt; and at the same time explaining the testing tools that &lt;code&gt;genix&lt;/code&gt; provide.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./counter&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="nx"&gt;genix&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;genix&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Counter&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should allow get the actual value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="c1"&gt;// using genix to build a wrapper &lt;/span&gt;
    &lt;span class="c1"&gt;// around the function tested&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// mocking getInitialValue command&lt;/span&gt;
    &lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getInitialValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// indicating that getValue will be executed&lt;/span&gt;
    &lt;span class="c1"&gt;// this is a lazy execution so for now nothing happen&lt;/span&gt;
    &lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// running our wrapper&lt;/span&gt;
    &lt;span class="kd"&gt;const&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="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;expect&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;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note in the previous test the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Always the function tested must be wrapped&lt;/strong&gt;, is this isn't done can occur some race conditions between tests.&lt;/li&gt;
&lt;li&gt;Every tests using &lt;code&gt;genix&lt;/code&gt; testing tools should be &lt;code&gt;async&lt;/code&gt; because the &lt;code&gt;run&lt;/code&gt; method return a promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onCommand&lt;/code&gt; method of our wrapper allow us mock commands that we have as dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exec&lt;/code&gt; method of our wrapper indicate a command that will be triggered against the function tested, this method can receive arguments after the command name.&lt;/li&gt;
&lt;li&gt;Before the &lt;code&gt;run&lt;/code&gt; call nothing happen, in this way can be said that that our wrapper behave lazily, for example &lt;code&gt;exec&lt;/code&gt; indicate a command that we can trigger but only as specification, only when &lt;code&gt;run&lt;/code&gt; is called is that actually the command is executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt; execution return a promise containing an object, this object has a &lt;code&gt;data&lt;/code&gt; property that represent the value returned by the last command triggered against of our function, in the previous tes was declared only one command &lt;code&gt;getValue&lt;/code&gt; to happen so data will be the return value of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's apply all of this to another test, and show how events can be emitted in our tests&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;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Counter&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should react to tick event correctly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;wrapper&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getInitialValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&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="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&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="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="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="nx"&gt;events&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;expect&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;toBe&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueUpdated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueUpdated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;toBe&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueUpdated&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="nf"&gt;toBe&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="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;genix&lt;/code&gt; wrappers expose a fluent API so the wrapper methods can be chained.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;run&lt;/code&gt; call besides &lt;code&gt;data&lt;/code&gt; also expose a &lt;code&gt;events&lt;/code&gt; property, this is a object in which every property correspond to an event emitted by our function during its execution.&lt;/li&gt;
&lt;li&gt;In this test the only event emitted was &lt;code&gt;valueUpdated&lt;/code&gt; so we have a property with the same name on &lt;code&gt;events&lt;/code&gt; this &lt;code&gt;valueUpdated&lt;/code&gt; property will be an array containing the list arguments used to emit this event, so &lt;code&gt;valueUpdated[0]&lt;/code&gt; contain the arguments used the first time when &lt;code&gt;valueUpdated&lt;/code&gt; was emitted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's finish with a simple test to check the behavior of &lt;code&gt;resetValue&lt;/code&gt; command.&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;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Counter&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should reset value correctly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;wrapper&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getInitialValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tick&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="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resetValue&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="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="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="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;expect&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;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summarizing &lt;code&gt;genix&lt;/code&gt; features that makes tests easier we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow different environment for every test .&lt;/li&gt;
&lt;li&gt;Commands used as dependencies can be mocked.&lt;/li&gt;
&lt;li&gt;Events can be emitted and commands triggered during testing.&lt;/li&gt;
&lt;li&gt;Inner events emitted during testing are fully exposed.&lt;/li&gt;
&lt;li&gt;Access to the result value of the last command executed in our chain of operation , make easier test side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Until this point was described the two main building blocks that &lt;code&gt;genix&lt;/code&gt; provide us to build event driven applications, they are events and commands. The idea of this post as I mentioned is show the public API of this library and its capabilities, in future posts I will show some more real world examples using it along with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; and also with &lt;a href="https://expressjs.com/es/" rel="noopener noreferrer"&gt;Express&lt;/a&gt; in the backend side. &lt;/p&gt;

&lt;p&gt;The advantages of &lt;code&gt;genix&lt;/code&gt; can be seen mainly in large application which involve the interaction of many components from severals domain, in this cases the coupling between different parts can be decreased a lot. This ideas of event driven should not be arbitrary applied because this can lead to more problems than it solve, so &lt;code&gt;genix&lt;/code&gt; can work as a tool but is important have a good architecture. &lt;/p&gt;

&lt;p&gt;The library is very new, it born from my own ideas onto how make my applications easier to test, feel free to try it and any kind of feedback or contribution is more than welcome, you can find the source code &lt;a href="https://github.com/omenlog/genix" rel="noopener noreferrer"&gt;here&lt;/a&gt;. There are many things to improve like documentation, type coverage, etc..., so stay tuned.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>events</category>
      <category>testing</category>
    </item>
    <item>
      <title>Do you know the most powerful feature of JS generators ?</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Tue, 28 Jul 2020 20:58:51 +0000</pubDate>
      <link>https://dev.to/omenlog/do-you-know-the-most-powerful-feature-of-js-generators-2766</link>
      <guid>https://dev.to/omenlog/do-you-know-the-most-powerful-feature-of-js-generators-2766</guid>
      <description>&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@pictagramar?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Amar Yashlaha&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/powerful?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In a previous &lt;a href="https://dev.to/omenlog/why-we-have-generators-3bg6"&gt;article&lt;/a&gt; I described the concept of generator in JS, there was explained the strong relation that exists between &lt;strong&gt;Iterators&lt;/strong&gt;, &lt;strong&gt;Iterables&lt;/strong&gt; and &lt;strong&gt;Generators&lt;/strong&gt;. Now in this post I want to focus on one specific feature that make generators unique inside of JS landscape, this is:&lt;/p&gt;

&lt;center&gt;
    &lt;h3&gt;Bidirectional communication&lt;/h3&gt;
&lt;/center&gt;

&lt;h2&gt;
  
  
  Push and Pull protocols
&lt;/h2&gt;

&lt;p&gt;In order to understand what is bidirectional communication(BC) first &lt;code&gt;Push&lt;/code&gt; and &lt;code&gt;Pull&lt;/code&gt; as communication protocols, between data producers and consumers should be understood.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;Pull&lt;/code&gt; the consumer is who determine when the data is received from the producer. Functions are the simpler example of pull in JS. For any function &lt;code&gt;F&lt;/code&gt; is true that it doesn't know when the data will be produced or in another way &lt;code&gt;F&lt;/code&gt; doesn't know when it will be executed, the consumer has all responsibility over the &lt;code&gt;F()&lt;/code&gt; call to pull some kind of data.&lt;/p&gt;

&lt;p&gt;In the other hand with &lt;code&gt;Push&lt;/code&gt; protocol the producer has full control over the moment when the data is produced, the consumer doesn't know neither when or how the data is produced.&lt;br&gt;
&lt;code&gt;Promises&lt;/code&gt; comply with this definition of &lt;code&gt;Push&lt;/code&gt;.&lt;br&gt;
For every promise &lt;code&gt;P&lt;/code&gt; a callback should be passed to its &lt;code&gt;then&lt;/code&gt; method in order to get the promise data asynchronously, later at some point this callback will be executed when the promise is fulfilled, in this case the callback doesn't know about how the data was produced, the inner implementation of &lt;code&gt;P&lt;/code&gt; determine when data is pushed to our callback.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Iterables are another example of &lt;code&gt;Pull&lt;/code&gt; and Observables works as a &lt;code&gt;Push&lt;/code&gt; mechanism.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Bidirectional communication using generators
&lt;/h2&gt;

&lt;p&gt;BC over generators is based on the fact that they support &lt;code&gt;Pull&lt;/code&gt; and &lt;code&gt;Push&lt;/code&gt; at the same time, or in other words generators can be at the same time data consumers and data producers.&lt;/p&gt;

&lt;p&gt;An example of generator as data producer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;producerGen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;producerGen&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="nx"&gt;it&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="c1"&gt;// {done: false, value:1 }&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;it&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="c1"&gt;// {done: false, value:2 }&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;it&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="c1"&gt;// {done: false, value:3 }&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example &lt;code&gt;producerGen&lt;/code&gt; is only acting as producer, the values are consumed inside of &lt;code&gt;consumer&lt;/code&gt; function, here we have a pulling happening through our &lt;code&gt;it&lt;/code&gt; variable. But a generator can consume data and producing it as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataFromOutSide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&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="nx"&gt;dataFromOutSide&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataFromGenerator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it&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;value&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="nx"&gt;dataFromGenerator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;

  &lt;span class="nx"&gt;it&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Analyzing this piece of code step by step, first iterator &lt;code&gt;it&lt;/code&gt; is obtained from &lt;code&gt;generator&lt;/code&gt; function.&lt;br&gt;
The first call to &lt;code&gt;it.next()&lt;/code&gt; run &lt;code&gt;generator&lt;/code&gt; till the point when it reach the &lt;code&gt;yield&lt;/code&gt; keyword, at this point the execution of &lt;code&gt;generator&lt;/code&gt; is paused and &lt;code&gt;1&lt;/code&gt; is send to outside, acting &lt;code&gt;generator&lt;/code&gt; in its roll as data producer. Then the value emitted from &lt;code&gt;generator&lt;/code&gt; is printed and &lt;code&gt;next&lt;/code&gt; is called again but passing an argument in the call &lt;code&gt;it.next(2)&lt;/code&gt;, when &lt;code&gt;next&lt;/code&gt; is called with an argument &lt;code&gt;generator&lt;/code&gt; execution is resumed, and also the previous &lt;code&gt;yield&lt;/code&gt; expression is replaced by the argument used in the call to &lt;code&gt;next&lt;/code&gt;, in this example &lt;code&gt;yield 1&lt;/code&gt; will be replaced by &lt;code&gt;2&lt;/code&gt; so the variable &lt;code&gt;dataFromOutside&lt;/code&gt; will receive &lt;code&gt;2&lt;/code&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%2Fokwsrqqtu4t9xl0533fy.gif" 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%2Fokwsrqqtu4t9xl0533fy.gif" alt="bidirectional flow" width="1197" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This gif show the communication flowing in both directions from side to side, so is clear how &lt;code&gt;generator&lt;/code&gt; produce and consume data, in fact &lt;code&gt;consumer&lt;/code&gt; function is also a producer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Advantages of Bidirectional Communication
&lt;/h2&gt;

&lt;p&gt;After understand this feature, someone might wonder &lt;em&gt;What are the benefits of bidirectional communication ?&lt;/em&gt;, and the answer is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;separation of concern&lt;/li&gt;
&lt;li&gt;inversion of control&lt;/li&gt;
&lt;li&gt;code easier to test&lt;/li&gt;
&lt;li&gt;high level of decoupling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As example I'll implement a function two times one using &lt;code&gt;async-await&lt;/code&gt; and another using generators, in order to analyze what is gained from bidirectional communication in the generator based implementation.&lt;br&gt;
Suppose a function to get user data that first check if the user is in cache else it request the data from server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userOnCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`user:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;userOnCache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userFromBackend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;userFromBackend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;userOnCache&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;&lt;em&gt;Error handling is not covered for simplicity&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thinking a moment about this function with unit tests in mind first thing to note is that &lt;code&gt;getUserData&lt;/code&gt; depends on &lt;code&gt;cache&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt;, is known that during unit tests should be avoided any call to backend and also any read against cache storage, therefore to test this function in isolation its dependencies should be mocked.&lt;br&gt;
But mocking is a big topic in software development, there are many libraries dedicated to make easier mocks creation and in other hand there are some opinions about &lt;a href="https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a" rel="noopener noreferrer"&gt;mocking as a code smell&lt;/a&gt;, besides all of this, developers claiming testing as a difficult task is a fact, mainly in situation when they have a implementation with high level of coupling and therefore should be implemented many mocks, this developers don't enjoy the testing process or worse they decide not to test the code at all.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;getUserData&lt;/code&gt; can be implemented using some sort of dependency injection making it easier to test but this topic is out of the scope.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After use &lt;code&gt;async-await&lt;/code&gt; and conclude that mocks are needed for unit test let's see what happen in the implementation using generators, for this &lt;code&gt;async&lt;/code&gt; will be replaced by &lt;code&gt;function*&lt;/code&gt; and every &lt;code&gt;await&lt;/code&gt; sentence by &lt;code&gt;yield&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userOnCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`user:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;userOnCache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userFromBackend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;userFromBackend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;userOnCache&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;Now &lt;code&gt;getUserData&lt;/code&gt; is a generator that will &lt;code&gt;yield&lt;/code&gt; promises. Write unit tests for this generator is simple, for example a test for the use case when we don't have user data in cache so we get our user from the server can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getUserData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./get-user-data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should get user data from backend when user isn't cached&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// fake user data&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jhon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// get an iterator from generator, remember this iterator will emit promises&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// run generator til the first yield&lt;/span&gt;
  &lt;span class="nx"&gt;it&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="c1"&gt;// resume generator execution passing undefined as data&lt;/span&gt;
  &lt;span class="nx"&gt;it&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="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// resume generator, passing to it userData simulating the server response,&lt;/span&gt;
  &lt;span class="c1"&gt;// also retrieve the next value emitted by it,&lt;/span&gt;
  &lt;span class="c1"&gt;// at this point value came from the return statement&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it&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;userData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// check that the correct data was returned&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This show how easy is to test the code using bidirectional communication. The difference with the first implementation is that with &lt;code&gt;async-await&lt;/code&gt; promises are send to JS engine and it will be in charge to resolve them and resume the function execution, that communication between the engine and our code can't be intercepted, so for test the function in isolation its dependencies should be mocked. In other hand generators give full control over the promises yielded by &lt;code&gt;getUserData&lt;/code&gt; so they can be intercepted allowing pass to our generator whatever kind of data, indeed &lt;code&gt;getUserData&lt;/code&gt; is totally unaware is the promise was resolved or if is fake data being injected.&lt;/p&gt;

&lt;p&gt;This test could seem very brittle, coupled to our implementation, because &lt;code&gt;next&lt;/code&gt; calls are linked to &lt;code&gt;yield&lt;/code&gt; statements of &lt;code&gt;getUserData&lt;/code&gt; also for every call to &lt;code&gt;next&lt;/code&gt; should be passed manually the correct type of data, having this as a consequence that a little change one the implementation might break the test. For sure this is true this test can be improved, but I'm only showing how powerful BC is, maybe I cover this topic in a future post.&lt;/p&gt;

&lt;p&gt;One drawback of generators is that with &lt;code&gt;async&lt;/code&gt; functions they can be invoked and the language knows how to execute them, awaiting and resuming promises automatically. The same isn't true for generators, I mean JS doesn't what kind of values generators will produce and what should be done with them, so we as developers are in charge to get data and resume the execution of our generators. But don't worry if we know what type of values will be yielded then we can implement a function that pull values from our generator and resume it automatically.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This idea of write generator runners is not new and is used by some &lt;a href="https://www.npmjs.com/package/co" rel="noopener noreferrer"&gt;libraries&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A siple &lt;code&gt;run&lt;/code&gt; function that can execute generators can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;iteratorResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it&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="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;iteratorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;iteratorResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;iteratorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;run&lt;/code&gt; will receive an &lt;code&gt;iterator&lt;/code&gt;, then get the first data using &lt;code&gt;next()&lt;/code&gt;, after that it will continue retrieving data from &lt;code&gt;iterator&lt;/code&gt; while it isn't done, for every piece of data we &lt;code&gt;await&lt;/code&gt; the property &lt;code&gt;value&lt;/code&gt; to resume our generator passing the promise &lt;code&gt;result&lt;/code&gt; in the &lt;code&gt;next&lt;/code&gt; call, by last we return the last value emitted by &lt;code&gt;iterator&lt;/code&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%2Fojqgug1446xx0q3j9knw.gif" 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%2Fojqgug1446xx0q3j9knw.gif" alt="Alt Text" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run can be used like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;userData&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;In summary this post explained very briefly &lt;code&gt;Pull&lt;/code&gt; and &lt;code&gt;Push&lt;/code&gt; as communication protocols also how bidirectional communication works on generators.&lt;br&gt;
We explored this feature transforming a generators in data producers and consumers. As example the behavior of &lt;code&gt;async-await&lt;/code&gt; was reproduced using generators, trying to exemplify how easy is build tests for a generator based implementation. This post isn't a comparative between generators and &lt;code&gt;async-await&lt;/code&gt;, both are powerful and I'm really glad that JS support them. Hopefully you understand the basics of BC after this read, in the future post I'll continue writing about it exposing what we can achieve.&lt;/p&gt;

&lt;p&gt;Thanks for read.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Converting decimals in Romans using FP</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Mon, 06 Jul 2020 21:10:41 +0000</pubDate>
      <link>https://dev.to/omenlog/converting-decimals-in-romans-using-fp-pik</link>
      <guid>https://dev.to/omenlog/converting-decimals-in-romans-using-fp-pik</guid>
      <description>&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@alschim?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Alexander Schimmeck&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/romans-numbers?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Let's explore how we can implement an algorithm that allow us convert a decimal number in it's roman representation. I like functional programming(FP) so also during the implementation I want use common concepts from FP like &lt;strong&gt;pure functions&lt;/strong&gt; and &lt;strong&gt;function composition&lt;/strong&gt; , so hopefully this also serve as example to show how you can apply FP to problem solving.&lt;/p&gt;

&lt;p&gt;We will develop a simple &lt;code&gt;converter&lt;/code&gt; function that will receive a decimal number as input and output the roman representation of our input, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1679&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MDCLXXIX&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;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;p&gt;Before deep dive in the implementation, let analyze step by step our conversion algorithm. &lt;/p&gt;

&lt;p&gt;First we should know what characters we have available in the roman numeric system and the decimal number that every one of them represent,we have the following set of characters:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For simplify our converter the algorithm will be for numbers less than &lt;code&gt;5000&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;center&gt;

| Roman | Decimal |
| ----- | ------- |
| M     | 1000    |
| CM    | 900     |
| D     | 500     |
| CD    | 400     |
| C     | 100     |
| XC    | 90      |
| L     | 50      |
| XL    | 40      |
| X     | 10      |
| IX    | 9       |
| V     | 5       |
| IV    | 4       |
| I     | 1       |

&lt;/center&gt;

&lt;blockquote&gt;
&lt;p&gt;As you see I represented some decimals using two letters example &lt;code&gt;CD&lt;/code&gt;, this is in order to avoid in our algorithm the subtractions that take place in the roman numeric system, think as this 2 characters as a whole that is associated with a decimal value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The next step is for every decimal number try to decompose it as a sum, using only the decimals number exposed previously, we should use the minimum number of operands in our sum, let's see:&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%2Fem6s87gn1ehlighjd5dz.png" 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%2Fem6s87gn1ehlighjd5dz.png" alt="decomposing" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, from this decomposition is very straightforward get the roman representation. So this is how our algorithm work, it will go from top to bottom over our available decimals and check if the roman token associated with it should be in our final representation and how many times we should include the respective token.&lt;/p&gt;

&lt;p&gt;Our algorithm will build the roman number in an incremental way, for check how many times a specific roman token should be present we use the &lt;code&gt;/&lt;/code&gt; operator in conjunction with the decimal representation of this token against our input, the &lt;code&gt;%&lt;/code&gt; operator is used in every step to get the remain that we will use as input when processing the next roman character, as we know an example is worth than thousand words so let see how we can transform &lt;code&gt;38&lt;/code&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%2Fgowvy6k9dnqzwbdk46su.png" 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%2Fgowvy6k9dnqzwbdk46su.png" alt="processX" width="800" height="573"&gt;&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%2Fkiya6roszip03bo8wg46.png" 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%2Fkiya6roszip03bo8wg46.png" alt="processIX" width="800" height="582"&gt;&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%2Fr1k2vkhs1nuf0e58j3yn.png" 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%2Fr1k2vkhs1nuf0e58j3yn.png" alt="processV" width="800" height="582"&gt;&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%2Frd4tvp7g9gpcficqutx7.png" 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%2Frd4tvp7g9gpcficqutx7.png" alt="processIV" width="800" height="578"&gt;&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%2Fus8izdor1qstfqgo6ghb.png" 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%2Fus8izdor1qstfqgo6ghb.png" alt="processI" width="800" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point we end and Roman = XXXVIII is our initial number represented using roman notation&lt;/p&gt;

&lt;p&gt;Note the following in our algorithm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We process roman characters from top to bottom staring from M to I.&lt;/li&gt;
&lt;li&gt;In every step we do exactly the same operations (&lt;code&gt;/&lt;/code&gt; , &lt;code&gt;concatenation&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;) over our arguments.&lt;/li&gt;
&lt;li&gt;We update in every steps our Roman representation concatenating new characters or maybe nothing.&lt;/li&gt;
&lt;li&gt;We update in every step our &lt;code&gt;input&lt;/code&gt; that will be used in the next step.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;/&lt;/code&gt; operation is used to find how many time a specific characters should be included in our representation.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;%&lt;/code&gt; operation is used to find the remaining amount that need to be converted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Now that we saw how the conversion algorithm works let's go through it's implementation.&lt;/p&gt;

&lt;p&gt;First I will start implement some utility functions that we will use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Divider
&lt;/h3&gt;

&lt;p&gt;As in every step &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;%&lt;/code&gt; operations are used let's start implementing a function that help us with this task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;cocient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Repeat
&lt;/h3&gt;

&lt;p&gt;We need a function that allow us repeat a character a specific amount of times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;repeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&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;h3&gt;
  
  
  Pipe
&lt;/h3&gt;

&lt;p&gt;As I mention earlier we will use function composition in the implementation, for this let's use a &lt;code&gt;pipe&lt;/code&gt; function. With &lt;code&gt;pipe&lt;/code&gt; we can for example write &lt;code&gt;g = arg =&amp;gt; f2(f1(arg))&lt;/code&gt; as &lt;code&gt;g = pipe(f1,f2)&lt;/code&gt;, in this example &lt;code&gt;g&lt;/code&gt; is composed by &lt;code&gt;f1&lt;/code&gt; and &lt;code&gt;f2&lt;/code&gt;, the out of &lt;code&gt;f1&lt;/code&gt; is passed as and argument to &lt;code&gt;f2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&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;fns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&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;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;f&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;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* 
    If you not follow the pipe implementation don't worry 
    just remind that this function serve 
    to pass the output of one function as input to another.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see the implementation, we know that during the conversion we did the same operation in every steps over our input, the only thing different was the roman character and the decimal that is represent. With this in mind let's build a &lt;code&gt;process&lt;/code&gt; function that receive as arguments a &lt;strong&gt;romanChar&lt;/strong&gt; and it's &lt;strong&gt;decimal&lt;/strong&gt; representation and return a function &lt;code&gt;F&lt;/code&gt; that will be responsible to run the conversion algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;romanChar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* function to check if our romanChar will we in our final representation */&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&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="cm"&gt;/*
        arg:{
          num: decimal number that we are converting
          roman: partial representation of our solution
        }
    */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;roman&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* num equal 0 imply that there is not anything to transform */&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cm"&gt;/* find how many time we should repeat romanChar and the remain that need to transform */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;cocient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/* get the new romans characters */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newRomanChars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cocient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;romanChar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/* update num as rest and update our actual roman representation concatenating newChars */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;roman&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;roman&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;newRomanChars&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="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;Ok until this point we have our &lt;code&gt;process&lt;/code&gt; function that allow us check if a specific roman character should be present in our final transformation for example &lt;code&gt;const f = process('V', 5)&lt;/code&gt; give us a function &lt;code&gt;f&lt;/code&gt; that should receive our &lt;code&gt;arg&lt;/code&gt; object and determine if &lt;code&gt;V&lt;/code&gt; should be included in our final solution.&lt;/p&gt;

&lt;p&gt;The last step is create a converter function composing different function where each one has&lt;br&gt;
only the responsibility to check one character, our transformation will be passed from one function to another.At the end we end with an object which &lt;code&gt;num&lt;/code&gt; is 0 and &lt;code&gt;roman&lt;/code&gt; is the full conversion,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;convert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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="na"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;roman&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;M&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CM&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;D&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;XC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;L&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;XL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IX&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;V&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IV&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;process&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I&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="nx"&gt;roman&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;roman&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how our &lt;code&gt;convert&lt;/code&gt; function receive a number and in the first step(first function) we transform it to our &lt;code&gt;arg&lt;/code&gt; shape so we can start the conversion, also in the last step we get our &lt;code&gt;arg&lt;/code&gt; object and extract from it &lt;code&gt;roman&lt;/code&gt; property with the full conversion.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Full source code can be found &lt;a href="https://github.com/omenlog/articles/blob/master/from-decimals-to-romans-a-functional-way/index.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Conclusions
&lt;/h1&gt;

&lt;p&gt;As we stated at the beginning we used &lt;code&gt;function composition&lt;/code&gt;, and &lt;code&gt;pure functions&lt;/code&gt; in the sense that none of our functions rely on side effects, in every step we don't modify our &lt;code&gt;arg&lt;/code&gt; instead we a create a new object, that will be passed to the next function in our chain.&lt;/p&gt;

&lt;p&gt;This example is simple but I hope that it give you some insights on how you can use this concepts in your every day tasks.&lt;/p&gt;

&lt;p&gt;This approach to build our &lt;code&gt;convert&lt;/code&gt; function in a declarative way give us as advantage that is easier adapt to new requirements, for example our &lt;code&gt;convert&lt;/code&gt; function can be refactored to work with numbers greater than &lt;code&gt;5000&lt;/code&gt; only adding another call without modify our &lt;code&gt;process&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Thanks for read&lt;/p&gt;

&lt;p&gt;If you like this article and want read more from me, you can follow &lt;a href="https://twitter.com/omenlog" rel="noopener noreferrer"&gt;me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Improve your commits using vscode</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Sun, 31 May 2020 12:43:09 +0000</pubDate>
      <link>https://dev.to/omenlog/improve-your-commits-using-vscode-cf6</link>
      <guid>https://dev.to/omenlog/improve-your-commits-using-vscode-cf6</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@yancymin" rel="noopener noreferrer"&gt;yancymin&lt;/a&gt; at &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are like me and enjoy Visual Studio Code(vscode) as a tool keep reading. Today I want show a simple trick using this editor to commit our code in a more organized way(in my opinion). I have been using this editor for almost 3 years, but it wasn't till some months ago that I figure it that I can commit my files partially, that means I can create a commit that doesn't include every changes that I made to a single files.&lt;/p&gt;

&lt;p&gt;I had to learn this because I was working in a project where the people in charge to review my code were very strict about the commits, things like the changes included were very important for them, in order to understand the work done by some developer. One of the main requirements was doesn't merge changes related to new features with changes regarding to refactorization, so in this way we separate concerns in our commits. At the beginning I struggled achieving this, but after learnt how files can be partially committed everything was easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Partially commits in vscode
&lt;/h2&gt;

&lt;p&gt;Suppose that we have the following changes:&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%2Fz80h5jk9w9axcstnw34n.png" 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%2Fz80h5jk9w9axcstnw34n.png" alt="gif diff" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see here we have implemented a new feature function and also refactored our &lt;code&gt;getSessionKey&lt;/code&gt; function, we want include this two changes in two different commits . &lt;/p&gt;

&lt;p&gt;In vscode with for do that we select the changes that we want include in the next commit doing right click on the diff view and &lt;code&gt;Stage Selected Ranges&lt;/code&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%2F8342pcvugl3n33faq666.png" 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%2F8342pcvugl3n33faq666.png" alt="Select ranges" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The editor allow us during the diff view, add changes on different selections, so we can select for example lines 5-10 and stage them, after that select lines 30-40 and stage them, repeating this until we have staged all the changes that we want commit.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After this we can see that our file is in the staging area ready for be included in our next commit, but note that we have staged only our change related to new feature. &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%2F90z0372b0s97745qdpgz.png" 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%2F90z0372b0s97745qdpgz.png" alt="Alt Text" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point we can proceed to commit and do the process again for the remaining changes. &lt;/p&gt;

&lt;p&gt;As you can see this is very easy and allow us to separate concern in our commits. I think that this is very helpful in many occasions when  we work and make many changes not being related or unable to live in the same commit, also with this approach we end with a detailed history of our work which for communicate our intentions and facilitates the code review process.&lt;/p&gt;

&lt;p&gt;If you know another useful trick that we can use in vscode share it in the comments, thanks for your read.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PS: I love the new UI for writing post thanks to &lt;a href="https://dev.to/devteam"&gt;@devteam&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>vscode</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>If you are looking for job, HackerRank offer industry-vetted skills directory</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Sat, 23 May 2020 19:58:28 +0000</pubDate>
      <link>https://dev.to/omenlog/hackerrank-offer-industry-vetted-skills-directory-31e5</link>
      <guid>https://dev.to/omenlog/hackerrank-offer-industry-vetted-skills-directory-31e5</guid>
      <description>&lt;p&gt;On May 21 HackerRank published a detailed library of in-demand technical skills. This directory came out with the objective to help in the creation of standardized interview process from the technical point of view. So after the required skills and competences being defined you as developer trying to get a job or as interviewer can be in the same page during the evaluation.&lt;/p&gt;

&lt;p&gt;For every skills there are defined 3 different categories Basic, Intermediate and Advanced,  and is detailed the require knowledge for every category. Among the skills we can find &lt;strong&gt;AWS, Node.js, Django, Docker, CSS, React&lt;/strong&gt; and many more. Also some skills have their own subset of question, which is be very helpful in my opinion for newcomers that sometime struggling regarding to what they should learn, also this questions can help more experienced developers in how they should prepare before a job interview.&lt;/p&gt;

&lt;p&gt;So if you are a searching for a job, are in charge of the interview process at some company or just simply want know what you should learn about some specific technology check this directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.hackerrank.com/skills-directory?utm_medium=content&amp;amp;utm_source=blog&amp;amp;utm_campaign=skillsdirectory" rel="noopener noreferrer"&gt;HackerRank Skills Directory&lt;/a&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Explaining JavaScript Generators</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Mon, 18 May 2020 00:46:19 +0000</pubDate>
      <link>https://dev.to/omenlog/why-we-have-generators-3bg6</link>
      <guid>https://dev.to/omenlog/why-we-have-generators-3bg6</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@jasonstrull" rel="noopener noreferrer"&gt;Jason Strull&lt;/a&gt; on &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today I want to explain my mental model about &lt;strong&gt;Generators&lt;/strong&gt;  in JavaScript. I'll try to do this mixing technical concepts with my own view of how they fit together. Summarizing I'll be talking about &lt;strong&gt;Iterators,Iterables&lt;/strong&gt; and &lt;strong&gt;Generators&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Very often I see some sort of confusion around generators concept, based in the fact that there are many terms and expressions used when developers speak about them,this make a little bit hard to figure it out what it's happening. I went through this confusion the first time that I ear about it, and the situation is worst for junior developers. The first pieces of information that I read about generators 2-3 years ago was somethings like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The are function that don't run-to-completion, they can stop its execution in the middle of the function body, and can be resumed later either right away or later in time.&lt;/li&gt;
&lt;li&gt;When you run a generator function &lt;code&gt;const it = gen()&lt;/code&gt; you actually don't run code in the generator instead you get and iterator but  if you log &lt;code&gt;it&lt;/code&gt; in the console you get &lt;code&gt;Object [Generator] {}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;They allow bidirectional communication improving the async flow control &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%2Fuploads%2Farticles%2Fh9hs6gpa4k92n6nqz6zq.gif" 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%2Fuploads%2Farticles%2Fh9hs6gpa4k92n6nqz6zq.gif" alt="wdf" width="350" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From that comments I had the following  issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why I want a function that doesn't run-to-completion ?&lt;/li&gt;
&lt;li&gt;I run the function but it in fact not ran.&lt;/li&gt;
&lt;li&gt;What is an generator object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though they were added on ES6, today I think that generators are a blurry concept for many developers, many don't use it or try to avoid, sometimes because they don't find a use case that is very well suited for generator or are developers that simply don't fully understand the concept. So let's begin with the explanations.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The concepts exposed here are relevant to JavaScript, they are available in another languages as well but I'm focus on its relevance for JS.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Iterator
&lt;/h2&gt;

&lt;p&gt;In my opinion for gain a clear understanding around &lt;strong&gt;Generators&lt;/strong&gt; we need to understand another underlying concepts related to them, and in that way conform the base over which generators are developed. The first of this concept is &lt;code&gt;Iterators&lt;/code&gt;. Adapting an simplifying the definition from the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; we have:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;iterator protocol&lt;/strong&gt; defines a standard way to produce a sequence of values (either finite or infinite). An &lt;strong&gt;object is an iterator&lt;/strong&gt; when it implements a &lt;code&gt;next()&lt;/code&gt; method that allow us consume the values from a container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So and iterator allow us produce and/or traverse values that belongs to a container, note that this container not necessarily must be a list,it can be an object, set, tree, graph, map or simply values generated on demand. The &lt;strong&gt;iterator protocol&lt;/strong&gt; mentioned in the definition give us and standard way to consume values, in summary the protocol define the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the values can be consumed calling the &lt;code&gt;next()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;next()&lt;/code&gt; method return and object with two properties:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;done&lt;/code&gt;: A boolean that indicates a completion status of the iteration, using this property the consumer is able to know if all the values were consumed or not.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt;: current iteration value or final return value&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &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;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;container&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;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="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;iterator&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="c1"&gt;// {done: false, value: 1}&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;iterator&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="c1"&gt;// {done: false, value: 2&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;iterator&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="c1"&gt;// {done: true, value: undefined}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in this example we have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;container&lt;/code&gt; &lt;strong&gt;array is not an iterator by itself&lt;/strong&gt; if we execute &lt;code&gt;container.next()&lt;/code&gt; we get &lt;code&gt;TypeError: container.next is not a function&lt;/code&gt;, see how the container doesn't obey the iterator protocol and doesn't know how its values can be consumed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;iterator&lt;/code&gt; object implement the &lt;strong&gt;iterator protocol&lt;/strong&gt; through it's &lt;code&gt;next&lt;/code&gt; method,allowing us consume &lt;code&gt;container&lt;/code&gt; array values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Iterables
&lt;/h2&gt;

&lt;p&gt;Now that we saw in brief the concept of Iterators lets talk about Iterables. As in the case of Iterators, based on the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; documentation we can define Iterables as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to be iterable, an object must implement the &lt;code&gt;[Symbol.iterator]&lt;/code&gt; method, the implementation must be a zero-argument function that returns an iterator.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If an object meets the previous definition then it's one iterable, and follow the &lt;strong&gt;iterable protocol&lt;/strong&gt;. This protocol is just an standard way to allow containers define or customize their iteration behavior. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Exist many tools built in the language that are integrated with this &lt;strong&gt;iterable protocol&lt;/strong&gt;, as example can be &lt;a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Sentencias/for...of" rel="noopener noreferrer"&gt;for...of&lt;/a&gt; loops, &lt;a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/Sintaxis_Spread" rel="noopener noreferrer"&gt;spread operator&lt;/a&gt;,etc... Also the main data structures of the language are &lt;strong&gt;iterables&lt;/strong&gt; by default, this is the case of String, Arrays, Sets, Maps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After all of this we can simply say that an &lt;strong&gt;iterable&lt;/strong&gt; has a method stored in a very specific property(&lt;code&gt;Symbol.iterator)&lt;/code&gt; that when is executed return an &lt;em&gt;iterator&lt;/em&gt; that can be used to consume the iterable values. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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="nx"&gt;arrayIterator&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="c1"&gt;// {value: 1, done: false}&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;arrayIterator&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="c1"&gt;// {value: 2, done: false}&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;arrayIterator&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="c1"&gt;// {value: undefined, done: true}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this snippet we consume the values of the array &lt;code&gt;iterable&lt;/code&gt;, but without implement by our self the iterator just using what the language provide to us.&lt;/p&gt;

&lt;p&gt;Let's see another example but now making our own iterable, we will make a plain object iterable and the iteration should will be over its properties, also lets be a little bit funny and implement a simple functions that allow us consume values from iterables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
  - The keys of an object can be retrieved using Object.keys 
    you know that,
    but this is a just a simple example.
*/&lt;/span&gt;

&lt;span class="cm"&gt;/* Function that allow another function consume an iterator */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;consumeIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumerFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iterResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iterator&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="cm"&gt;/*
    Note that this function is very naive, 
    and assume that when the iterator is done its value is undefined 
  */&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iterResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;consumerFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iterResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;consumeIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumerFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* Function that allow another function consume an iterable */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;consumeIterable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumerFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt;
  &lt;span class="nf"&gt;consumeIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumerFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Iterable consumed&lt;/span&gt;&lt;span class="se"&gt;\n&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="cm"&gt;/* by default object literals are not iterables */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;objectIterable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&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="na"&gt;baz&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="cm"&gt;/* lets add our special property to make it iterable */&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/* Consume our iterable object using our new helper function */&lt;/span&gt;

&lt;span class="nf"&gt;consumeIterable&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;objectIterable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* Consume the object again but now applying a different consumer function */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logUpperCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nf"&gt;consumeIterable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logUpperCase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;objectIterable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this piece of code the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo
baz
Iterable consumed

FOO
BAZ
Iterable consumed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code has a plenty of comments, anyway if you have a question don't hesitate on leave it in the comments section. In the previous example we were able to write functions that work over any iterable/iterator thanks  to the protocols.&lt;/p&gt;

&lt;p&gt;If  the implementation for &lt;code&gt;[Symbol.iterator]&lt;/code&gt; in our iterable object looks a little bit confusing you can read my previous &lt;a href="https://dev.to/omenlog/javascript-why-this-1lf0"&gt;article&lt;/a&gt; about how &lt;code&gt;this&lt;/code&gt; behave in JS to reach a better understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generators
&lt;/h2&gt;

&lt;p&gt;OK so far we saw that iterators allow us consume values from some specific container, and iterables define an common interface to expose iterators so ...What about generators ?&lt;/p&gt;

&lt;h4&gt;
  
  
  Generators are a simply and very straightforward way of &lt;strong&gt;generate&lt;/strong&gt; iterables and iterators.
&lt;/h4&gt;

&lt;p&gt;For me this is how I visualize generators, they are a function that define how values from some iterable are emitted, I think that is more easy see them as iterables that we want use in our application, and from there about a generator that emit those values, rather than not thinking about functions that doesn't fully run to completion and other stuffs regarding generators, at least in order to start using them . I'm not saying that is wrong all of the other facts about this concept indeed they are correct, I'm just only exposing how is easier for me think about iterables consumption.&lt;/p&gt;

&lt;p&gt;Some advantages of generators are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They return an object(&lt;code&gt;Object [Generator]&lt;/code&gt;) that is &lt;code&gt;iterator&lt;/code&gt; and &lt;code&gt;iterable&lt;/code&gt; at the same time.&lt;/li&gt;
&lt;li&gt;The values returned or yielded from the generator are automatically wrapped as an object that meet the iterator protocol.&lt;/li&gt;
&lt;li&gt;With them is easier to keep iterator inner state without necessity of extra variables&lt;/li&gt;
&lt;li&gt;Generators allow inject data before create the iterator making the whole process more dynamic.&lt;/li&gt;
&lt;li&gt;They allow communication in both direction acting as a pulling and pushing mechanism at the same time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;lets see one example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* a function generators is declared using function* */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="cm"&gt;/*yield mean a pause point and emit a value that can be consumed */&lt;/span&gt;

  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="cm"&gt;/* look how iter is an iterator */&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;iter&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="c1"&gt;// {value: 1, done: false}&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;iter&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="c1"&gt;// {value: 2, done: false}&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;iter&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="c1"&gt;// {value: undefined, done: true}&lt;/span&gt;

&lt;span class="cm"&gt;/* at the same time the value returned by the generator is an iterable */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iterator1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newIt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iterator1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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="nx"&gt;newIt&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="c1"&gt;// {value: 1, done: fasle}&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;newIt&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="c1"&gt;// {value: 2, done: false}&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;newIt&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="c1"&gt;// {value: undefined, done: true}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to note here are how we are yielding a number but the consumer get an object  under the &lt;strong&gt;iterator protocol&lt;/strong&gt;, and for show that the generator also return an &lt;code&gt;iterable&lt;/code&gt; we call again the &lt;code&gt;gen&lt;/code&gt; generator, we do this to avoid extract the iterator directly from the &lt;code&gt;iter&lt;/code&gt; iterable because when an iterator generated reach its done state it remain there in every successive call to &lt;code&gt;next&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets recreatee our previous example related to object keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;genObjectKeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* pausing point, 
       inner state of the loop is automatically manage by the interpreter */&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* we can dinamicaly inject the object at creation time*/&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;genObjectKeys&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;foo&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="na"&gt;baz&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="cm"&gt;/* we can use our previous helper, this is a huge advantage that protocols give us */&lt;/span&gt;

&lt;span class="nf"&gt;consumeIterator&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So with this we saw a very basic overview of how iterators, iterables, and generators are in a very strict relation. There are many aspects regarding  this concepts that I skipped in order to cover the basics, somethings like async iterators, iterators composition, bidirectional communication using generators, etc ... &lt;/p&gt;

&lt;p&gt;Anyway if your are interested in some of this topics or want see a  more realistic example using generators let me know in the comments. &lt;/p&gt;

&lt;p&gt;Thank you for read&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Deno is coming</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Sun, 10 May 2020 17:45:26 +0000</pubDate>
      <link>https://dev.to/omenlog/deno-is-coming-18fg</link>
      <guid>https://dev.to/omenlog/deno-is-coming-18fg</guid>
      <description>&lt;p&gt;The creator of &lt;a href="https://nodejs.org/es/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; from some time has been working in a new project called &lt;a href="https://deno.land/" rel="noopener noreferrer"&gt;Deno&lt;/a&gt;, the first stable version of this project should be published in 3 days on May 13. &lt;/p&gt;

&lt;p&gt;The project is defined as&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Deno is a JavaScript/TypeScript runtime with secure defaults and a great developer experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the &lt;a href="https://github.com/denoland/deno" rel="noopener noreferrer"&gt;official repo&lt;/a&gt; we can summarize some of the main features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Supports TypeScript out of the box.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Has built-in utilities.&lt;/strong&gt;: It include a dependency inspector(&lt;code&gt;deno info&lt;/code&gt;), code formatter(&lt;code&gt;deno fmt&lt;/code&gt;), test runner(&lt;code&gt;deno test&lt;/code&gt;), bundler(&lt;code&gt;deno bundle&lt;/code&gt;), documentation generator(&lt;code&gt;deno doc&lt;/code&gt;), debugger&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ships a single executable.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scripts can be bundled into a single javascript file.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secure by default&lt;/strong&gt;: This mean that unlike Node when we run our applications they by default don't have access to the file system, network or enviroment, for that we need enable them  using some flags as command line options to allow access for example &lt;code&gt;deno --allow-read=/etc&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Some difference with Node are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deno doesn't use &lt;code&gt;npm&lt;/code&gt;, it uses modules referenced as URLs or file paths&lt;/li&gt;
&lt;li&gt;Deno doesn't use &lt;code&gt;package.json&lt;/code&gt; in its module resolution algorithm.&lt;/li&gt;
&lt;li&gt;All async actions in Deno return a promise. &lt;strong&gt;Thus Deno provides different APIs than Node.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Explicit permissions.&lt;/li&gt;
&lt;li&gt;Deno always dies on uncaught errors.&lt;/li&gt;
&lt;li&gt;Uses &lt;code&gt;ES Modules&lt;/code&gt; and doesn't support &lt;code&gt;require()&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This and more information can be found on the official repo I expose it here just in order to show a quick resume. So now that we know main features, and differences what are your thoughts regarding it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Will you give a try it in upcoming projects ?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Do you think it will cause a huge change in the Node community&lt;/em&gt;&lt;br&gt;
&lt;em&gt;In your opinion what is the best feature&lt;/em&gt;&lt;br&gt;
&lt;em&gt;What your dislike most about it&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Above I share some of the main question that I hear when I talk about it with some fellows at work. I'll be glad to read your opinions in the comments.&lt;/p&gt;

&lt;p&gt;Thanks in advance&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>node</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>JavaScript why 'this'</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Sat, 02 May 2020 18:30:23 +0000</pubDate>
      <link>https://dev.to/omenlog/javascript-why-this-1lf0</link>
      <guid>https://dev.to/omenlog/javascript-why-this-1lf0</guid>
      <description>&lt;p&gt;Recently I was working on a project with Angular 7 when a teammate told me about a problem he had within a component, the problem was that he subscribed to an observable returned by one service implemented in the application and within the callback in charge of receiving the values did not have access to an attribute defined in the component.&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%2Fuploads%2Farticles%2Fxgqha12avfr6xrb241go.png" 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%2Fuploads%2Farticles%2Fxgqha12avfr6xrb241go.png" alt="Initial Code" width="800" height="726"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Specifically, the problem was on line 14 that &lt;code&gt;data&lt;/code&gt; isn't defined as property of &lt;code&gt;this&lt;/code&gt; 🙄🤔.&lt;/p&gt;

&lt;p&gt;After analyzing the code I told him that the problem wasn't related to Angular, and to understand the cause of it he should know how &lt;code&gt;this&lt;/code&gt; binding works in JavaScript. A few days after I told about this error to another colleague, and while I explain it I realized that he also didn't have a complete understanding of how &lt;code&gt;this&lt;/code&gt; work.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the article &lt;code&gt;this&lt;/code&gt; is used to represent the JavaScript keyword and not the English word&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Currently JavaScript is within one of the most used languages worldwide, I think it's very common to find developers working with JavaScript that use &lt;code&gt;this&lt;/code&gt; without really understanding the basics of this feature that the language provides us. I think this is largely due to the introduction of pseudo-classes in ES6, since they try to imitate a similar syntax for the definition of &lt;strong&gt;classes&lt;/strong&gt; to that of other languages, and therefore less experienced developers tend to associate &lt;code&gt;this&lt;/code&gt; in the same way that it works in other programming languages (my colleagues had worked with PHP and C# respectively).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; binding
&lt;/h2&gt;

&lt;p&gt;The keyword &lt;code&gt;this&lt;/code&gt; in JavaScript is automatically defined inside of the scope of any function &lt;code&gt;f&lt;/code&gt;, and within each &lt;code&gt;f&lt;/code&gt; we have that &lt;code&gt;this&lt;/code&gt; represents a given object. The problem really with &lt;code&gt;this&lt;/code&gt; is that the object represented is not defined by the way we implement the function, but is defined dynamically at run time depending on how we call the function, that is, the object represented by this hasn't nothing to do with where &lt;code&gt;f&lt;/code&gt; is declared, but it has to do with the way &lt;code&gt;f&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;Simplifying we can say that the interpreter uses 5 rules to determine the object that &lt;code&gt;this&lt;/code&gt; represents within &lt;code&gt;f&lt;/code&gt;, we will explain each of these rules and then define their precedence levels.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default binding
&lt;/h3&gt;

&lt;p&gt;The first rule that we will examine is the simplest of all, and applies whenever one of the others is not applicable, so we can also say that it is the rule of least precedence.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;default binding&lt;/strong&gt; is applied when a function &lt;code&gt;f&lt;/code&gt; is called in the form &lt;code&gt;f()&lt;/code&gt;. When this rule is applied &lt;code&gt;this&lt;/code&gt; points to the global scope, note that this has the consequence that if we modify &lt;code&gt;this&lt;/code&gt; within the function for example by inserting some property it will be accessible even after executing the function because it would be defined globally, for example:&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%2Fuploads%2Farticles%2Fv5ciomg1cjlyrxwat87d.png" 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%2Fuploads%2Farticles%2Fv5ciomg1cjlyrxwat87d.png" alt="default binding" width="800" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It is valid to clarify that the variable &lt;code&gt;name&lt;/code&gt; within the global scope is accessible only in the case of browsers, for the case of Node on line 6 it would have been printed &lt;code&gt;undefined&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the previous snippet it is exemplified as &lt;code&gt;this&lt;/code&gt; points to the global scope.&lt;/p&gt;

&lt;p&gt;In the case that we execute our script in &lt;code&gt;strict mode&lt;/code&gt; at the time of applying the default binding the interpreter doesn't allow this to represent the global scope, therefore this will point to undefined. The previous snippet running in &lt;code&gt;strict mode&lt;/code&gt; throw the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: Cannot read property 'name' of undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implicit binding
&lt;/h3&gt;

&lt;p&gt;The second rule or &lt;strong&gt;implicit binding&lt;/strong&gt; is applied in the case that a function &lt;code&gt;f&lt;/code&gt; contained in an &lt;code&gt;obj&lt;/code&gt; object is executed using dot notation for its execution &lt;code&gt;obj.f()&lt;/code&gt;, example:&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%2Fuploads%2Farticles%2Foqjylco3y9scdsdgszjz.png" 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%2Fuploads%2Farticles%2Foqjylco3y9scdsdgszjz.png" alt="implicit binding" width="800" height="796"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous example we see how both objects contain &lt;code&gt;printInfo&lt;/code&gt; property that refers to the same function, but despite this when executing the function in one case &lt;code&gt;this&lt;/code&gt; represent the &lt;code&gt;dwarf&lt;/code&gt; object, while for the other it's  &lt;code&gt;threeEyesRaven&lt;/code&gt;. This is because in each of the calls to the function an object is used, which we can name as &lt;code&gt;context&lt;/code&gt;, in this case the &lt;strong&gt;implicit binding&lt;/strong&gt; define that within the function &lt;code&gt;this&lt;/code&gt; points to the context object, therefore saying &lt;code&gt;this.name&lt;/code&gt; would be the same as saying &lt;code&gt;dwarf.name&lt;/code&gt; or &lt;code&gt;threeEyesRaven.name&lt;/code&gt; depending on the object used in the call.&lt;/p&gt;

&lt;h4&gt;
  
  
  Lost Implicity
&lt;/h4&gt;

&lt;p&gt;It's very common for some developers to lose at some point in the source code the &lt;strong&gt;implicit binding&lt;/strong&gt; defined for some specific object, which means that the binding that is applied would be the default binding having &lt;code&gt;this&lt;/code&gt; pointing to the global scope or &lt;code&gt;undefined.&lt;/code&gt; This can happen when we use callbacks, for example:&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%2Fuploads%2Farticles%2F18hsoso7c6asp6pvlc5a.png" 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%2Fuploads%2Farticles%2F18hsoso7c6asp6pvlc5a.png" alt="lost biding" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens is that here we are passing directly to &lt;code&gt;setTimeout&lt;/code&gt; a reference to our function &lt;code&gt;printInfo&lt;/code&gt; without passing the object where it's contained, on the other hand we have no control of how &lt;code&gt;setTimeout&lt;/code&gt; call the function, to better understand what happens suppose this pseudo implementation of &lt;code&gt;setTimeout&lt;/code&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%2Fuploads%2Farticles%2Fe4gk2uxph7nkbatstpvl.png" 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%2Fuploads%2Farticles%2Fe4gk2uxph7nkbatstpvl.png" alt="pseudo set timeout" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Analyzing the call-site of &lt;code&gt;fn&lt;/code&gt; in the previous snippet is easy to conclude that the default binding is applied and the explicit binding that was previously available is lost, because dot notation isn't used to call the function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explicit Binding
&lt;/h3&gt;

&lt;p&gt;So far we have seen 2 rules to determine the value of &lt;code&gt;this&lt;/code&gt; within a function, the first applies when we call the function as standalone function and the second when the function is executed by accessing it as part of an object.&lt;/p&gt;

&lt;p&gt;Next we will see another type of binding for the case in which we explicitly define the object to which &lt;code&gt;this&lt;/code&gt; points within a function, this type of binding is known as &lt;strong&gt;explicit binding&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To get into the explanation of this type of binding we must start talking about 2 methods present in every JavaScript functions, these methods are &lt;strong&gt;apply&lt;/strong&gt; and &lt;strong&gt;call&lt;/strong&gt;. Both methods take the object to be pointed by &lt;code&gt;this&lt;/code&gt; as the first parameter and then execute the function with this configuration. Because we directly indicate what will be the value for &lt;code&gt;this&lt;/code&gt; when executing the function we are in presence of &lt;strong&gt;explicit binding&lt;/strong&gt;. For example:&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%2Fuploads%2Farticles%2Ffmoadxz89smln2kfu7tj.png" 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%2Fuploads%2Farticles%2Ffmoadxz89smln2kfu7tj.png" alt="expliciti binding" width="800" height="629"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous example, we noticed how the first time we executed the &lt;code&gt;print&lt;/code&gt; function, it print "Rob Stark" because that is the value of the name property of &lt;code&gt;kingInTheNorth&lt;/code&gt; object which contains the &lt;code&gt;print&lt;/code&gt; function and therefore applying the &lt;strong&gt;implicit binding&lt;/strong&gt; when executing the function &lt;code&gt;this&lt;/code&gt; will point to the object. The second time we execute the function then "Jon Snow" is printed instead of Rob Stark even though we are accessing to the same function contained in the kingInTheNorth object, what happens is that in the function's call-site we are calling the &lt;code&gt;call&lt;/code&gt; method and explicitly indicating that the function is executed using the &lt;code&gt;newKing&lt;/code&gt; object as &lt;code&gt;this&lt;/code&gt;, so in that case within the function &lt;code&gt;this.name&lt;/code&gt; refers to &lt;code&gt;newKing.name&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Explicit binding with &lt;code&gt;bind&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Sometimes is desirable to indicate &lt;code&gt;this&lt;/code&gt; for some function without executing it. For this case, each function has a &lt;code&gt;bind&lt;/code&gt; method which, like &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt;, takes as its first parameter the object that &lt;code&gt;this&lt;/code&gt; will represent but instead of executing the function &lt;code&gt;bind&lt;/code&gt; returns a new function with &lt;code&gt;this&lt;/code&gt; already linked to the specified object, let's look at the following example:&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%2Fuploads%2Farticles%2Ffc6nsh2v0rtcq13xe9p8.png" 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%2Fuploads%2Farticles%2Ffc6nsh2v0rtcq13xe9p8.png" alt="using bind" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we see from the same &lt;code&gt;house&lt;/code&gt; function two new functions were created through the use of &lt;code&gt;bind&lt;/code&gt;, using in each case different objects to represent &lt;code&gt;this&lt;/code&gt;, note how in the &lt;code&gt;bind&lt;/code&gt; call the &lt;code&gt;house&lt;/code&gt; function is not executed at any time, in this way at the end we have created a house for the Targaryen and a house for the Tyrell.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;new&lt;/code&gt; Binding
&lt;/h3&gt;

&lt;p&gt;To understand the new binding we must know what happens when a function is called preceded by &lt;code&gt;new&lt;/code&gt;, in this case the following occurs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A new object is created.&lt;/li&gt;
&lt;li&gt;The new object is linked to the prototype of the function executed.&lt;/li&gt;
&lt;li&gt;The new object created is set as &lt;code&gt;this&lt;/code&gt; within that function.&lt;/li&gt;
&lt;li&gt;Unless the function returns something different, the new object is automatically returned by the function.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;For simplicity, let's completely ignore the step 2 it and let's focus on the others&lt;/p&gt;
&lt;/blockquote&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%2Fuploads%2Farticles%2F6zt5yu0w6ix7xzjmyle0.png" 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%2Fuploads%2Farticles%2F6zt5yu0w6ix7xzjmyle0.png" alt="new operator" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we see how each time the function is invoked using &lt;code&gt;new&lt;/code&gt;, a new object is created on each call, this object is automatically returned from the &lt;code&gt;King&lt;/code&gt; function even though it doesn't have return statement.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Currently the vast majority of developers don't use &lt;code&gt;new&lt;/code&gt; to execute functions but when invoking es6 classes, in this example a function were used because we are analyzing the behavior of this within functions , although classes are at the end functions as well 😎😉.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Arrow functions
&lt;/h3&gt;

&lt;p&gt;A new way of declaring functions was introduced in ES6(&lt;strong&gt;arrow functions&lt;/strong&gt;), to declare a function in this way we use the operator &lt;code&gt;=&amp;gt;&lt;/code&gt;, for example:&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%2Fuploads%2Farticles%2Fjwa05bqiqtmvznelo1xy.png" 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%2Fuploads%2Farticles%2Fjwa05bqiqtmvznelo1xy.png" alt="arrow function" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the features of this approach is that the functions alter the behavior of &lt;code&gt;this&lt;/code&gt;, so that it's not dynamic depending on the function's call-site, but is lexical. In a simplified way, &lt;code&gt;this&lt;/code&gt; within an arrow function represents the same object that it represented in the parent scope that contains the defined function, that is, the arrow function inherits &lt;code&gt;this&lt;/code&gt; from the enclosing scope, example:&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%2Fuploads%2Farticles%2Fddnvw5adwie76vtdjtb3.png" 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%2Fuploads%2Farticles%2Fddnvw5adwie76vtdjtb3.png" alt="arrow function2" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Observe that when the timer is executed we don't lose the reference of &lt;code&gt;this&lt;/code&gt; pointing to  &lt;code&gt;HouseStark&lt;/code&gt; object, which happens  in case that we pass an anonymous function &lt;code&gt;function(){}&lt;/code&gt; to &lt;code&gt;setTimeout&lt;/code&gt;, the above is because we are using an arrow function as timer first argument. The parent scope in this example is defined by the &lt;code&gt;printMembers&lt;/code&gt; function, when executing this function from the &lt;code&gt;HouseStark&lt;/code&gt; object, the implicit binding is applied and &lt;code&gt;this&lt;/code&gt; will be the object itself, as a consequence then &lt;code&gt;this&lt;/code&gt; within the &lt;strong&gt;arrow function&lt;/strong&gt; will be &lt;code&gt;HouseStark&lt;/code&gt; object so we can access to all its properties without problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Determining &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To know what &lt;code&gt;this&lt;/code&gt; represent within a function we first find the call-site of that function, remember that this depends directly on the way in which the function is executed, then we follow this steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;(&lt;strong&gt;new binding&lt;/strong&gt;) Is the function called using &lt;code&gt;new&lt;/code&gt; ? If so, &lt;code&gt;this&lt;/code&gt; points to a new empty object built before executing the function.&lt;/li&gt;
&lt;li&gt;(&lt;strong&gt;explicit binding&lt;/strong&gt;) Is the function executed using &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt; ? &lt;code&gt;this&lt;/code&gt; points to an object explicitly specified as the first parameter of the call.&lt;/li&gt;
&lt;li&gt;(&lt;strong&gt;implicit binding&lt;/strong&gt;) Is the function executed by accessing it through an object that contain it ? In that case, &lt;code&gt;this&lt;/code&gt; represent the object that contains the function as one of its properties.&lt;/li&gt;
&lt;li&gt;(&lt;strong&gt;default binding&lt;/strong&gt;) In another case &lt;code&gt;this&lt;/code&gt; points to the global scope or &lt;code&gt;undefined&lt;/code&gt; if we are in &lt;code&gt;strict mode&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In case of arrow functions then &lt;code&gt;this&lt;/code&gt; will be inherited from the enclosing scope, and this in the enclosing scope is determined following the previous steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;As a recap we can say that &lt;code&gt;this&lt;/code&gt; in JavaScript is dynamically defined at run time, depending on the call-site of a function. There are 4 different types of binding. Using arrow functions we can say that it would not have its own &lt;code&gt;this&lt;/code&gt; but inherits it from the enclosing scope.&lt;/p&gt;

&lt;p&gt;Now that we have talked in detail about &lt;code&gt;this&lt;/code&gt; , would you know what is the problem in the code published initially 👍&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more information on the subject I highly recommend this  &lt;a href="https://www.amazon.com/-/es/Kyle-Simpson/dp/1491904151" rel="noopener noreferrer"&gt;book&lt;/a&gt; from Kyle Simpson &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Any recommendations do not hesitate to leave your comments, thank you for read&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>poo</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What happen with echojs.com</title>
      <dc:creator>Omar E. Lopez</dc:creator>
      <pubDate>Wed, 14 Feb 2018 14:53:51 +0000</pubDate>
      <link>https://dev.to/omenlog/what-happen-with-echojscom--9ja</link>
      <guid>https://dev.to/omenlog/what-happen-with-echojscom--9ja</guid>
      <description>&lt;p&gt;Yesterday in the morning as always I went to check &lt;a href="http://echojs.com" rel="noopener noreferrer"&gt;echojs.com&lt;/a&gt; ,website which read a lot of what was happening in the javascript environment and I found this&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%2Fuploads%2Farticles%2Fj2vrndb0jccf17elzmek.png" 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%2Fuploads%2Farticles%2Fj2vrndb0jccf17elzmek.png" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Someone knows because the site is not publishing the news as they have always done...??&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
