<?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: Pratik Chaudhari</title>
    <description>The latest articles on DEV Community by Pratik Chaudhari (@pratikgchaudhari).</description>
    <link>https://dev.to/pratikgchaudhari</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%2F315415%2F07e0685c-5fa2-4b73-a62a-cd39d06598c0.jpg</url>
      <title>DEV Community: Pratik Chaudhari</title>
      <link>https://dev.to/pratikgchaudhari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratikgchaudhari"/>
    <language>en</language>
    <item>
      <title>The choice of Tech Stack is overrated!</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sat, 15 Mar 2025 04:07:25 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/the-choice-of-tech-stack-is-overrated-5922</link>
      <guid>https://dev.to/pratikgchaudhari/the-choice-of-tech-stack-is-overrated-5922</guid>
      <description>&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%2F45jutx47r5mxvy1f5bot.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45jutx47r5mxvy1f5bot.jpg" alt="A meme depicting Developer's over focus on Tech Stack rather than the product that they are building" width="500" height="659"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think, as developers, we focus too much on the Tech Stack.&lt;/p&gt;

&lt;p&gt;Your tech stack is just raw materials.&lt;/p&gt;

&lt;p&gt;If the roof of your house starts leaking after a light drizzle, does it matter that it was built using the most expensive cement out there?&lt;/p&gt;

&lt;p&gt;Similarly, if a company fails to monetise its product (and goes under as a result) that it built using the latest, greatest, and the hottest programming language / web framework / database that's out there, and goes under as a result, does anything actually matter, let alone the choice of their tech stack?&lt;/p&gt;

&lt;p&gt;Shortly after its collapse, people will forget that the company ever existed, and no one will care what it did and how it did it.&lt;/p&gt;

&lt;p&gt;On the contrary, if the same company succeeds, and its founders &amp;amp; employees become millionaires overnight, its tech stack will be treated as the holy grail of success!&lt;/p&gt;

&lt;p&gt;Why the difference?&lt;/p&gt;

&lt;p&gt;Why should the choice of the Tech stack matter in the latter case, if it doesn't matter in the former?&lt;/p&gt;

&lt;p&gt;Does the success of a company using a specific technology really guarantee the success of others who follow in its footsteps?&lt;/p&gt;

&lt;p&gt;Over the past decade or so, we have seen dozens of companies fail miserably.&lt;/p&gt;

&lt;p&gt;They tried to copy the successful incumbents in the markets.&lt;/p&gt;

&lt;p&gt;They used the same technology under the hood as their successful counterparts. Some of them even went so far as to poach the developers from their rivals!&lt;/p&gt;

&lt;p&gt;These developers brought in their "tech expertise" from their previous employer, and yet, their new employer's product failed to gain traction in the market, ultimately leading to its inevitable death!&lt;/p&gt;

&lt;p&gt;What does this mean?&lt;/p&gt;

&lt;p&gt;Does it mean that the developers became "less capable" when their employer changed? Does it mean that the technology lost its "magic charm" as soon as it was removed from premises of the successful company?&lt;/p&gt;

&lt;p&gt;Is the technology like Rapunzel's hair? Does it lose its magical  properties as soon as it's cut?&lt;/p&gt;

&lt;p&gt;No, it's not.&lt;/p&gt;

&lt;p&gt;In fact, I would go so far as to say that it's a "superstition" to believe that a specific technology could guarantee success.&lt;/p&gt;

&lt;p&gt;It's the same thing as believing that your marriage will succeed  just because you married on a specific date or that crossing paths with a black cat will bring bad luck!&lt;/p&gt;

&lt;p&gt;We fail to acknowledge the fact that the Tech Stack is merely a means to an end and not the end in itself!&lt;/p&gt;

&lt;p&gt;There are many other aspects to a success story that are simply not known to the public at large for one reason or another.&lt;/p&gt;

&lt;p&gt;As a result, people simply attribute the success to aspects that they can see, and it's most primarily the tech stack!&lt;/p&gt;

&lt;p&gt;The Tech stack is just a small cog in the wheel.&lt;/p&gt;

&lt;p&gt;Frankly, it's nothing more than an investment.&lt;/p&gt;

&lt;p&gt;What really matters is the return on investment! And if that's zero, no one gives a shit!&lt;/p&gt;

</description>
      <category>tech</category>
      <category>techstack</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Implementing Your Own Event Loop From Scratch</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sun, 21 May 2023 15:09:47 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/implementing-your-own-event-loop-from-scratch-44l9</link>
      <guid>https://dev.to/pratikgchaudhari/implementing-your-own-event-loop-from-scratch-44l9</guid>
      <description>&lt;p&gt;Hello Folks,&lt;/p&gt;

&lt;p&gt;I've always wondered how an &lt;strong&gt;&lt;em&gt;Event Loop&lt;/em&gt;&lt;/strong&gt; works.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"How does it accept a task? How does it delegate that task?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"How does it continue accepting new tasks without pausing the execution of previously accepted tasks?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"How does it know that a previous task has completed its execution and the result should be returned to the caller?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Given that the &lt;strong&gt;Event Loop&lt;/strong&gt; sits at the core of languages such as &lt;code&gt;JavaScript&lt;/code&gt; and non-blocking client server &lt;code&gt;Java&lt;/code&gt; frameworks such as &lt;code&gt;Netty&lt;/code&gt; that are widely used in our day-to-day development, I felt that it's essential to gain a first-hand understanding of its inner workings!&lt;/p&gt;

&lt;p&gt;And, hence, I decided to implement an Event Loop from scratch in &lt;code&gt;Java!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here's how I went ahead with it...  &lt;/p&gt;

&lt;p&gt;I started with one of the core building blocks of an Event Loop: the &lt;code&gt;Event&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;By definition, an &lt;code&gt;Event&lt;/code&gt; in a software system is an entity that denotes some sort of activity or a change in the system.&lt;/p&gt;

&lt;p&gt;This change might be introduced due a user action or it might be  even triggered due a system activity that runs in the background (such as a CRON job).&lt;/p&gt;

&lt;p&gt;Events that are produced by a system might be consumed (or processed) by the same system internally or relayed to an external system for further processing.&lt;/p&gt;

&lt;p&gt;In either case, the event must contain sufficient information that enables its consumers to process it independently!&lt;/p&gt;

&lt;p&gt;Hence, for the sake our implementation, we'll assume that an event holds the following fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;asynchronous&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;asynchronous&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asynchronous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asynchronous&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the above class, one might wonder what's the significance of the &lt;code&gt;asynchronous&lt;/code&gt; field?&lt;/p&gt;

&lt;p&gt;Usually, there's a single thread or a process (depending on implementation) backing an Event Loop, i.e., the code of the Event Loop is executed by this single thread or process. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For the sake of simplicity and to avoid unnecessary repetition, we'll assume that a &lt;code&gt;thread&lt;/code&gt; is roughly equivalent to a &lt;code&gt;process&lt;/code&gt; and henceforth shall use the term &lt;code&gt;"thread"&lt;/code&gt; alone. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can call this thread as the &lt;code&gt;main&lt;/code&gt; thread.&lt;/p&gt;

&lt;p&gt;If this &lt;code&gt;main&lt;/code&gt; thread were to accept &amp;amp; execute a task by itself, it would have to block before accepting a new task, thus resulting in reduced throughput.&lt;/p&gt;

&lt;p&gt;Hence, instead of executing the task by itself, the main thread &lt;em&gt;"delegates"&lt;/em&gt; the task to a &lt;code&gt;worker&lt;/code&gt; thread.&lt;/p&gt;

&lt;p&gt;If we assume that an event is generated for each such task, we can say that the event is processed &lt;code&gt;asynchronously&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, there might be certain tasks that are extremely &lt;code&gt;"lightweight"&lt;/code&gt;, meaning executing these tasks won't consume much time, and hence, these could be executed &lt;code&gt;synchronously&lt;/code&gt; by the &lt;code&gt;main&lt;/code&gt; thread.&lt;/p&gt;

&lt;p&gt;To represent such tasks, we need a &lt;code&gt;differentiator&lt;/code&gt; in their events to treat them differently from the other tasks.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;asynchronous&lt;/code&gt; field in the &lt;code&gt;Event&lt;/code&gt; class serves this purpose!&lt;/p&gt;

&lt;p&gt;Now that we are clear with that, let's move ahead and implement the next building block (and probably the most important one), i.e., the &lt;code&gt;EventLoop&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's fill in all the details in the above class line by line...&lt;/p&gt;

&lt;p&gt;As we know, an event loop doesn't execute all the tasks itself.&lt;/p&gt;

&lt;p&gt;Instead, it delegates these to a &lt;code&gt;worker thread&lt;/code&gt; that runs in the &lt;em&gt;background&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But until that &lt;em&gt;delegation&lt;/em&gt; happens the Event Loop needs to store these tasks somewhere.&lt;/p&gt;

&lt;p&gt;And hence, it needs some sort of data structure to hold this data.&lt;/p&gt;

&lt;p&gt;Let's declare a field called &lt;code&gt;events&lt;/code&gt; that plays the role of an event store, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the sake of simplicity, we are assuming that the incoming tasks are prioritised for execution in a &lt;code&gt;"first come, first serve"&lt;/code&gt; manner, and hence, the &lt;code&gt;events&lt;/code&gt; field in the above class is of type &lt;code&gt;Deque&lt;/code&gt;, which is a type of &lt;code&gt;Queue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, the Event Loop needs to know what code to execute in response to an event generated for a given task, i.e., it needs another data structure to &lt;code&gt;map&lt;/code&gt; an event to its respective handler!&lt;/p&gt;

&lt;p&gt;And the same can be done as shown in the following code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;handlers&lt;/code&gt; field in the above code snippet is defined as a &lt;code&gt;Map&lt;/code&gt; of &lt;code&gt;String&lt;/code&gt; &amp;amp; &lt;code&gt;Function&lt;/code&gt;, wherein the &lt;code&gt;String&lt;/code&gt; serves as a unique identifier for an event and the &lt;code&gt;Function&lt;/code&gt; holds the code that will serve as a &lt;code&gt;handler&lt;/code&gt; (or a processor) for the said event.&lt;/p&gt;

&lt;p&gt;Now that we know where to store the events and how to map them to their handlers, are we done here?&lt;/p&gt;

&lt;p&gt;Certainly not!&lt;/p&gt;

&lt;p&gt;Once the &lt;code&gt;event handlers&lt;/code&gt; complete their execution, the results that they generate must be stored somewhere.&lt;/p&gt;

&lt;p&gt;But why do we need separate storage for the results? &lt;/p&gt;

&lt;p&gt;Why can't the results be sent directly to the &lt;code&gt;callers&lt;/code&gt; who had submitted the tasks?&lt;/p&gt;

&lt;p&gt;Remember that a majority of the tasks submitted to the Event Loop are executed &lt;code&gt;asynchronously&lt;/code&gt; by worker threads that run in the background. &lt;/p&gt;

&lt;p&gt;The sole job of these &lt;code&gt;worker threads&lt;/code&gt; is to complete the tasks and send a &lt;code&gt;signal&lt;/code&gt; to the &lt;code&gt;main&lt;/code&gt; thread stating the same.&lt;/p&gt;

&lt;p&gt;Now, what would be an effective way of &lt;em&gt;signalling&lt;/em&gt; to the &lt;em&gt;main&lt;/em&gt; thread that a given task is complete without making any sort of &lt;em&gt;blocking&lt;/em&gt; call?&lt;/p&gt;

&lt;p&gt;The answer is simple: Use another queue to store the results. &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;main&lt;/em&gt; thread would be then periodically poll this queue to check for new results.&lt;/p&gt;

&lt;p&gt;Let's do that by adding a new field called &lt;code&gt;‌processedEvents&lt;/code&gt; to our &lt;code&gt;EventLoop&lt;/code&gt; class as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's add a default &lt;em&gt;constructor&lt;/em&gt; to the above class to initialise all the fields mentioned above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;processedEvents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far so good, now that we have all the data structures in place, let's think about &lt;em&gt;wiring&lt;/em&gt; the events with their respective handlers.&lt;/p&gt;

&lt;p&gt;Let's add a method to the above class that does this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;processedEvents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;on()&lt;/code&gt; method in the above code populates the &lt;strong&gt;&lt;em&gt;handlers&lt;/em&gt;&lt;/strong&gt;  fields with an identifier for a given event and the code that should be executed in response to that event.&lt;/p&gt;

&lt;p&gt;Next, we need &lt;em&gt;schedule&lt;/em&gt; the event for execution, and that can be done by adding another method &lt;code&gt;dispatch()&lt;/code&gt; as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;processedEvents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so now we have a neat looking class with all the essential fields and the code to populate those fields.&lt;/p&gt;

&lt;p&gt;Will this be enough?&lt;/p&gt;

&lt;p&gt;No, not really, we still need some code that will take each event and call its event handler to execute the task for which that event was generated.&lt;/p&gt;

&lt;p&gt;So, let's create a method called &lt;code&gt;run()&lt;/code&gt; to do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nReceived Event: %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asynchronous&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;processAsynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;processSynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nEvent Loop was blocked for %s ms due to this operation %n%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;between&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endTime&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toMillis&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No handler found for %s%n%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;processedEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedEvent&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedEvent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what is this &lt;code&gt;run()&lt;/code&gt; method doing exactly?&lt;/p&gt;

&lt;p&gt;Let's dissect it line by line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first thing that it needs to do is to check if there are any events that haven't been &lt;em&gt;"processed"&lt;/em&gt; yet. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And to do that, it uses the &lt;code&gt;pop()&lt;/code&gt; method of the &lt;code&gt;events&lt;/code&gt; field. This method removes the first event in the queue and returns it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's possible that the &lt;code&gt;pop()&lt;/code&gt; method might return nothing (&lt;code&gt;null&lt;/code&gt;) if there are no &lt;em&gt;unprocessed&lt;/em&gt; events, hence the &lt;code&gt;run()&lt;/code&gt; method needs to ensure that it has retrieved a valid event before processing it further.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the retrieved event is valid, &lt;code&gt;run()&lt;/code&gt; prints a message stating the same (useful for debugging purposes, of course).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, it tries to find the &lt;code&gt;event handler&lt;/code&gt; for the retrieved event in the &lt;code&gt;handlers&lt;/code&gt; field.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the &lt;code&gt;event handler&lt;/code&gt; is found, &lt;code&gt;run()&lt;/code&gt; needs to decide how to execute it: &lt;code&gt;synchronously&lt;/code&gt; or &lt;code&gt;asynchronously&lt;/code&gt;?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To make this decision, it uses the &lt;code&gt;asynchronous&lt;/code&gt; field of the event and invokes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;processAsynchronously()&lt;/code&gt; if &lt;code&gt;asynchronous&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;processSynchronously()&lt;/code&gt; if &lt;code&gt;asynchronous&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;But what do these methods do? Let's have a look at their code:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processAsynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)))).&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processSynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;processAsynchronously()&lt;/code&gt; method creates a new &lt;code&gt;worker&lt;/code&gt; thread using the &lt;code&gt;Thread&lt;/code&gt; class of &lt;code&gt;Java&lt;/code&gt; and passes a &lt;code&gt;lambda&lt;/code&gt; function (denoted by &lt;code&gt;() -&amp;gt;&lt;/code&gt;) to it that acts a &lt;code&gt;runnable&lt;/code&gt;, i.e., code that can be executed concurrently. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And what does the &lt;code&gt;lambda&lt;/code&gt; function do?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, it creates a new instance of the &lt;code&gt;EventResult&lt;/code&gt; class (which represents a &lt;em&gt;"processed event"&lt;/em&gt;) using the event's unique identifier, i.e., &lt;code&gt;event.key&lt;/code&gt; and the  value produced by the &lt;code&gt;event handler&lt;/code&gt; mapped against the event and adds it to the list of &lt;code&gt;processedEvents&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;processAsynchronously()&lt;/code&gt; method then invokes the &lt;code&gt;start()&lt;/code&gt; method of this newly created &lt;code&gt;Thread&lt;/code&gt; to send a signal to the &lt;code&gt;Java Virtual Machine&lt;/code&gt; that this thread needs to be executed without blocking the calling thread (in this case the &lt;code&gt;main&lt;/code&gt; thread which is currently executing the code of the &lt;code&gt;processAsynchronously()&lt;/code&gt; method).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here's a simplified version of the above code for better understanding:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;eventHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eventHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;processedEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedEvent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="o"&gt;}).&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In case you haven't noticed, the &lt;code&gt;event handler&lt;/code&gt; is actually invoked using its &lt;code&gt;apply()&lt;/code&gt; method, and the event's data is passed to it as an argument. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But since this invocation happens inside the &lt;code&gt;lambda&lt;/code&gt; that's passed to the new &lt;code&gt;Thread&lt;/code&gt; instance, the &lt;code&gt;main&lt;/code&gt; thread isn't blocked by it, and hence we can say that the event is processed &lt;strong&gt;&lt;em&gt;"asynchronously!"&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the other hand, the &lt;code&gt;processSynchronously()&lt;/code&gt; simply invokes another method, &lt;code&gt;produceOutputFor()&lt;/code&gt; with a new instance of the &lt;code&gt;EventResult&lt;/code&gt; class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And the &lt;code&gt;produceOutputFor()&lt;/code&gt; simply prints the result of the processed event as illustrated below:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nOutput for Event %s : %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;However, unlike the &lt;code&gt;processAsynchronously()&lt;/code&gt; method, since the &lt;code&gt;processSynchronously()&lt;/code&gt; method invokes the &lt;code&gt;apply()&lt;/code&gt; method of the &lt;code&gt;event handler&lt;/code&gt; directly in the &lt;code&gt;main&lt;/code&gt; thread, the code contained in the event handler and the code inside the &lt;code&gt;produceOutputFor()&lt;/code&gt; is executed by the &lt;code&gt;main&lt;/code&gt; thread only, thereby resulting in the &lt;strong&gt;blocking&lt;/strong&gt; of the &lt;code&gt;main&lt;/code&gt; thread.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hence, in this case, we can say that event is executed &lt;strong&gt;&lt;em&gt;"synchronously!"&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that we are capturing the &lt;code&gt;startTime&lt;/code&gt; and &lt;code&gt;endTime&lt;/code&gt; in the &lt;code&gt;run()&lt;/code&gt; method above and then computing the difference between and printing it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nEvent Loop was blocked for %s ms due to this operation %n%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;between&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;endTime&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toMillis&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll understand the importance of the above code once we start executing our &lt;code&gt;EventLoop&lt;/code&gt;. Until then, let's move ahead...&lt;/p&gt;

&lt;p&gt;This is what the final &lt;code&gt;EventLoop&lt;/code&gt; class looks like with all the methods described so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;event.loop.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Duration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Instant&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.HashMap&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Map&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Deque&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.function.Function&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Deque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;processedEvents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nReceived Event: %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asynchronous&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;processAsynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;processSynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;

                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nEvent Loop was blocked for %s ms due to this operation %n%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                        &lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;between&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endTime&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toMillis&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No handler found for %s%n%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;processedEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedEvent&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedEvent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processAsynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processedEvents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)))).&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processSynchronously&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;produceOutputFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EventResult&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nOutput for Event %s : %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that all the basic building blocks of our Event Loop are ready, let's create a small &lt;code&gt;App&lt;/code&gt; to put it to some use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;event.loop.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;utils.Utils&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.BufferedReader&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.IOException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.InputStreamReader&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="n"&gt;eventLoop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Utils&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InputStreamReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;eventId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="nl"&gt;outer:&lt;/span&gt;
            &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What kind of task would you like to submit to the Event Loop?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 1. Wish me Hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 2. Print the contents of a file named hello.txt"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 3. Retrieve the latest news from New York Times &amp;amp; print it"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 4. Print output of previously submitted asynchronous task"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 5. Exit!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" &amp;gt; "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;usersChoice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;operationType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userHasNotChosenToExit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usersChoice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;usersChoice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

                    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"How would like to execute this operation?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                            &lt;span class="s"&gt;" 1. Synchronously (this would block the Event Loop until the operation completes)"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 2. Asynchronously (this won't block Event Loop in any way)"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" &amp;gt; "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

                    &lt;span class="n"&gt;operationType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;

                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;uniqueEventKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

                &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usersChoice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                        &lt;span class="n"&gt;uniqueEventKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generateUniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventId&lt;/span&gt;&lt;span class="o"&gt;++);&lt;/span&gt;
                        &lt;span class="n"&gt;eventLoop&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello! %s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you doing today?"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                                        &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                        &lt;span class="n"&gt;uniqueEventKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generateUniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"read-file"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventId&lt;/span&gt;&lt;span class="o"&gt;++);&lt;/span&gt;
                        &lt;span class="n"&gt;eventLoop&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;utils:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;readFile&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                                        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                                                &lt;span class="s"&gt;"hello.txt"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                                                &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                        &lt;span class="n"&gt;uniqueEventKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generateUniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fetch-latest-news"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventId&lt;/span&gt;&lt;span class="o"&gt;++);&lt;/span&gt;
                        &lt;span class="n"&gt;eventLoop&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;utils:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;fetchLatestNewsItemFromNewYorkTimes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                                        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getenv&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"API_KEY"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                                        &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;

                &lt;span class="n"&gt;eventLoop&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's deep dive into the above code to understand what's happening there...&lt;/p&gt;

&lt;p&gt;The first thing that we are doing is creating a new instance of our &lt;code&gt;EventLoop&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;EventLoop&lt;/span&gt; &lt;span class="n"&gt;eventLoop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventLoop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we are creating a new instance of a class called &lt;code&gt;Utils&lt;/code&gt; that contains many utility functions as depicted below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.fasterxml.jackson.databind.ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;event.loop.demo.NewsItems&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;okhttp3.OkHttpClient&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;okhttp3.Request&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.File&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.FileNotFoundException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.IOException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Random&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Utils&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Random&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Random&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getClassLoader&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getResource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileURL&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileURL&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getFile&lt;/span&gt;&lt;span class="o"&gt;())))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

                &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasNextLine&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FileNotFoundException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;fetchLatestNewsItemFromNewYorkTimes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;latestNews&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;httpClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OkHttpClient&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;newsItemIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://api.nytimes.com/svc/topstories/v2/technology.json?api-key=%s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;httpClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newCall&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="nc"&gt;NewsItems&lt;/span&gt; &lt;span class="n"&gt;newsItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;readValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;byteStream&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;NewsItems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newsItemsAreAvailable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;latestNews&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                        &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s - %s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newsItemIndex&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newsItemIndex&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;byLine&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;latestNews&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to get latest news"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;latestNews&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;generateUniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;humanReadableEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;eventCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s-%s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;humanReadableEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventCount&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;newsItemsAreAvailable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NewsItems&lt;/span&gt; &lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newsItems&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;results&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;newsItems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;userHasNotChosenToExit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userChoice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;userChoice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's have a look at each method in the &lt;code&gt;Utils&lt;/code&gt; class to gain a better understanding of the role that it plays in the grand scheme of things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;readFile(fileName)&lt;/code&gt; - This method will try to read a file whose name is specified as a parameter. For simplicity, we have assumed that the file will be located in the &lt;code&gt;resources&lt;/code&gt; folder of this &lt;code&gt;Java&lt;/code&gt; project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fetchLatestNewsItemFromNewYorkTimes()&lt;/code&gt; - This method retrieves the latest news from &lt;code&gt;The New York Times&lt;/code&gt; website using their public API.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;generateUniqueEventKey(humanReadableEventKey,eventCount)&lt;/code&gt; - This method creates a unique &lt;code&gt;key&lt;/code&gt; for every event. It does this by using the &lt;code&gt;humanReadableEventKey&lt;/code&gt; and the &lt;code&gt;eventCount&lt;/code&gt; parameters that are passed to it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;newsItemsAreAvailable(newsItems)&lt;/code&gt; - This method ensures that the data returned by the &lt;code&gt;The New York Times&lt;/code&gt; API is non-empty and non-null (we don't want any &lt;code&gt;NullPointerException&lt;/code&gt; or &lt;code&gt;IndexOutOfBoundsException&lt;/code&gt; to crash our code).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isAsynchronous(operationType)&lt;/code&gt; - This methods determines whether user wants the &lt;code&gt;EventLoop&lt;/code&gt; to execute a given operation &lt;code&gt;synchronously&lt;/code&gt; or &lt;code&gt;asynchronously&lt;/code&gt; (we'll get more clarity on this as we proceed with the explanation of the &lt;code&gt;App&lt;/code&gt; class).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;userHasNotChosenToExit(userChoice)&lt;/code&gt; - It determines whether the end-user has chosen to exit the program or to continue further.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Coming back to the &lt;code&gt;main()&lt;/code&gt; method in the &lt;code&gt;App&lt;/code&gt; class, we are seeking user's input via the command line against the following choices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 1. Wish me Hello
 2. Print the contents of a file named hello.txt
 3. Retrieve the latest news from New York Times &amp;amp; print it
 4. Print output of previously submitted asynchronous task
 5. Exit! 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User can choose to execute these tasks (except the last two) &lt;code&gt;synchronously&lt;/code&gt; or &lt;code&gt;asynchronously&lt;/code&gt;, and the methods of the &lt;code&gt;Utils&lt;/code&gt; class described above help with the same!&lt;/p&gt;

&lt;p&gt;When user submits a task and specifies its execution mode (&lt;code&gt;synchronous&lt;/code&gt; or &lt;code&gt;asynchronous&lt;/code&gt;), we submit it to the &lt;code&gt;EventLoop&lt;/code&gt; using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;eventLoop&lt;/span&gt;
 &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventHandlerForTheTask&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eventData&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;eventHandlerForTheTask&lt;/code&gt; in the above code snippet is a placeholder that for the actual event handler that we create based on the type of task chosen by the user, and &lt;code&gt;eventData&lt;/code&gt; acts as placeholder for the actual data required to perform the task.&lt;/p&gt;

&lt;p&gt;For example, if the user chose the following task...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Retrieve the latest news from New York Times &amp;amp; print it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...then the code for submitting it to the event loop would look something like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="n"&gt;eventLoop&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  
       &lt;span class="nl"&gt;utils:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;fetchLatestNewsItemFromNewYorkTimes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uniqueEventKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getenv&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"API_KEY"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;  
       &lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAsynchronous&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operationType&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above case, the &lt;code&gt;fetchLatestNewsItemFromNewYorkTimes()&lt;/code&gt; method of the &lt;code&gt;Utils&lt;/code&gt; class serves as an event handler and the &lt;code&gt;API_KEY&lt;/code&gt; is the data that's required for retrieving the latest new from &lt;code&gt;The New York Times&lt;/code&gt; API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;API_KEY&lt;/code&gt; can be obtained from &lt;a href="https://developer.nytimes.com/apis" rel="noopener noreferrer"&gt;Developer Portal of the New York Times&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once the event is &lt;code&gt;dispatched&lt;/code&gt;, we invoke the &lt;code&gt;run()&lt;/code&gt; method of the &lt;code&gt;EventLoop&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;eventLoop&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Invoking the &lt;code&gt;run()&lt;/code&gt; method starts a &lt;strong&gt;&lt;em&gt;"new iteration"&lt;/em&gt;&lt;/strong&gt; of the &lt;code&gt;EventLoop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright! &lt;/p&gt;

&lt;p&gt;Enough with all the theory, it's time to see our &lt;code&gt;EventLoop&lt;/code&gt; in action:&lt;/p&gt;

&lt;p&gt;To start the execution, let's fire up the &lt;code&gt;Terminal&lt;/code&gt; and execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;/gradlew&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--console&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;plain&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fzbfsnx77wte52e946hce.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%2Fzbfsnx77wte52e946hce.png" alt="Starting the Event Loop, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;Next, let's select the first task (&lt;code&gt;"Wish me Hello"&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%2Fnumrr983d8q29z8wsdzg.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%2Fnumrr983d8q29z8wsdzg.png" alt="Selecting the First Task, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;Now, let's tell our &lt;code&gt;EventLoop&lt;/code&gt; to execute it &lt;code&gt;synchronously&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%2F9m3mbssrul58m8gr6dxq.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%2F9m3mbssrul58m8gr6dxq.png" alt="Instructing Event Loop to Execute the First Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;The results:&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%2F3llcokhxwwbp0zr8pmxi.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%2F3llcokhxwwbp0zr8pmxi.png" alt="Results of Executing the First Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;As we can see from the above screenshot, executing the first task synchronously blocked the &lt;code&gt;EventLoop&lt;/code&gt; for about &lt;strong&gt;0 ms&lt;/strong&gt;.&lt;br&gt;
Also, notice that a unique id has been assigned to the event generated for this task: &lt;code&gt;hello-0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, let's choose the second task (&lt;code&gt;"Print the contents of a file named hello.txt"&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%2F5w01r1is0v9a3640t1ut.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%2F5w01r1is0v9a3640t1ut.png" alt="Selecting the Second Task, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;Let's tell our &lt;code&gt;EventLoop&lt;/code&gt; to execute it &lt;code&gt;synchronously&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%2Fy6njzdahxqhzz7i9vdzl.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%2Fy6njzdahxqhzz7i9vdzl.png" alt="Instructing Event Loop to Execute the Second Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;The results:&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%2Ftikxwo5q8vpbwf89uvoa.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%2Ftikxwo5q8vpbwf89uvoa.png" alt="Results of Executing the Second Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;As we can see from the above screenshot, executing the second task synchronously blocked the &lt;code&gt;EventLoop&lt;/code&gt; for about &lt;strong&gt;9 ms&lt;/strong&gt; or &lt;strong&gt;0.009 seconds&lt;/strong&gt;, and the unique id that's assigned to its event is &lt;code&gt;read-file-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, let's choose the third task (&lt;code&gt;"Retrieve the latest news from New York Times &amp;amp; print it"&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%2Fvo54qpmcbi01wzweougo.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%2Fvo54qpmcbi01wzweougo.png" alt="Selecting the Third Task, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;As with the other two tasks, we again instruct our &lt;code&gt;EventLoop&lt;/code&gt; to execute it &lt;code&gt;synchronously&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%2F4jzzsgk38jqefoxiult8.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%2F4jzzsgk38jqefoxiult8.png" alt="Instructing Event Loop to Execute the Third Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;The results:&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%2F0toonvmwv2fmcbhvdn5l.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%2F0toonvmwv2fmcbhvdn5l.png" alt="Results of Executing the Third Task Synchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;As we can see from the above screenshot, executing the event with id &lt;code&gt;fetch-latest-news-2&lt;/code&gt; synchronously blocked the &lt;code&gt;EventLoop&lt;/code&gt; for about &lt;strong&gt;3835 ms&lt;/strong&gt; or &lt;strong&gt;3.8 seconds.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, it seems printing a simple message on the screen takes the smallest amount of time whereas fetching data from the Internet takes the longest!&lt;/p&gt;

&lt;p&gt;Now, let's make one small change to the tasks that took more than &lt;strong&gt;0 ms:&lt;/strong&gt; to execute: let's switch the execution mode of these tasks to &lt;code&gt;asynchronous&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's do this for the second task: &lt;code&gt;"Print the contents of a file named hello.txt"&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%2F3x12o5bbd31dcrqcvjm4.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%2F3x12o5bbd31dcrqcvjm4.png" alt="Printing the contents of a file named hello.txt Asynchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;As we can see in the above screenshot, the &lt;code&gt;EventLoop&lt;/code&gt; was blocked for &lt;strong&gt;0 ms&lt;/strong&gt; this time! Also, notice that this time the event id has changed to &lt;code&gt;read-file-3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But wait, where's the output of the task?&lt;/p&gt;

&lt;p&gt;Here's the catch: since the &lt;code&gt;main&lt;/code&gt; thread delegated the task to a &lt;code&gt;worker&lt;/code&gt; thread, it won't have access to the output until the  &lt;code&gt;worker&lt;/code&gt; thread finishes its execution.&lt;/p&gt;

&lt;p&gt;So, how will the &lt;code&gt;main&lt;/code&gt; thread come to know that the &lt;code&gt;worker&lt;/code&gt; thread has finished the execution of the task?&lt;/p&gt;

&lt;p&gt;It will have to check the list of &lt;code&gt;processedEvents&lt;/code&gt; and look for the results stored against the unique id of the event generated for this task: &lt;code&gt;read-file-3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But in order to do this, the &lt;code&gt;EventLoop&lt;/code&gt; will have to execute another iteration.&lt;/p&gt;

&lt;p&gt;But how will it execute another iteration if it's waiting for the user to give it another task?&lt;/p&gt;

&lt;p&gt;Well, it won't!&lt;/p&gt;

&lt;p&gt;We'll have to give it a special task (&lt;code&gt;"Print output of previously submitted asynchronous task"&lt;/code&gt;) so that it is forced to execute another iteration and, in the process, picks up the result of the previous task.&lt;/p&gt;

&lt;p&gt;This is what it looks like:&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%2Fu46570xbsqqpwiw69elh.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%2Fu46570xbsqqpwiw69elh.png" alt="Retrieving the output of the previous task that was executed asynchronously, Image Credit: Pratik Chaudhari" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: Pratik Chaudhari



&lt;p&gt;That's it, folks!&lt;/p&gt;

&lt;p&gt;I hope you found this exercise as useful as I did.&lt;/p&gt;

&lt;p&gt;In case anyone wants to fiddle around with the code, feel free to fork the following repo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/pratikgchaudhari/event-loop-demo" rel="noopener noreferrer"&gt;https://github.com/pratikgchaudhari/event-loop-demo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>eventloop</category>
      <category>singlethreaded</category>
      <category>workerthreads</category>
      <category>nonblocking</category>
    </item>
    <item>
      <title>10 Hilarious Cartoons That Depict Real-Life Problems of Programmers</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sat, 06 Nov 2021 10:28:15 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/10-hilarious-cartoons-that-depict-real-life-problems-of-programmers-1hhi</link>
      <guid>https://dev.to/pratikgchaudhari/10-hilarious-cartoons-that-depict-real-life-problems-of-programmers-1hhi</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;And how they deal with them...&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  1. Estimations:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://levelup.gitconnected.com/the-art-of-eta-6ec69664ee89" rel="noopener noreferrer"&gt;&lt;em&gt;Estimating the amount of time required to complete your own work is nothing short of a herculean feat.&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the fun begins when you add more people to the equation.&lt;/p&gt;

&lt;p&gt;Then estimating becomes a sport of throwing darts (and that too in the dark):&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%2Fwwzk9fx9hniyaxrzxel9.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%2Fwwzk9fx9hniyaxrzxel9.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  2. Fixing Bugs:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/mind-cafe/expecting-immediate-results-might-hold-you-back-from-achieving-your-dreams-597f8785dc18" rel="noopener noreferrer"&gt;Humans prefer immediate gratification&lt;/a&gt;, which means we choose to indulge (or invest) in activities that could provide us with immediate rewards while conveniently overlooking the (heavy) price we may have to pay in the future.&lt;/p&gt;

&lt;p&gt;But when we do this in our workplaces, we might not be ones paying the price in the end:&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%2Fvzellj3nvuoawj1uxnc2.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%2Fvzellj3nvuoawj1uxnc2.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  3. Dealing With Ambitious Sales Rep:
&lt;/h3&gt;

&lt;p&gt;Sales representatives have targets to meet. We understand that.&lt;/p&gt;

&lt;p&gt;But when some overzealous sales rep promises the moon to the clients, programmers are the ones who bear the burnt:&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%2Ft0lwf8jaitehwc479kov.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%2Ft0lwf8jaitehwc479kov.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt; &lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  4. Socializing:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Blessed are the Geek, for they shall reign over the world."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Tech industry has had a tremendous impact on our lives: people can look up anything on Google and hook up with anyone on the likes of Tinder and Bumble.&lt;/p&gt;

&lt;p&gt;As people who built the tech industry, we are its Alpha &amp;amp; Omega.&lt;/p&gt;

&lt;p&gt;And yet, when it comes to in-person interactions and shaping conversations, we are at a loss for words:&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%2F9ypr3496mjdmwkka9gad.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%2F9ypr3496mjdmwkka9gad.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt; &lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  5. Finding a new Job:
&lt;/h3&gt;

&lt;p&gt;There are literally hundreds of websites out there that promise to join a potential employee and an employer into holy matrimony 😂&lt;/p&gt;

&lt;p&gt;But none of them can help you escape recruiters who sound more like sales reps. Deciphering the actual meaning behind their words is somewhat of an arcane art:&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%2Fdylav697gieh02mexnjp.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%2Fdylav697gieh02mexnjp.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  6. Feature Requests:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"The 'stories' feature is so popular on the XYZ app. We should introduce it too."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"What if our users could find a date, book a cab, and rent a villa in Europe all at the same time?"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And so goes the discussion about the new features that we could provide to our users.&lt;/p&gt;

&lt;p&gt;We understand that there's a need to experiment and evolve, but adding a feature just of the sake of adding it is not cool:&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%2Ff6jbjhcd09adsazyz9gk.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%2Ff6jbjhcd09adsazyz9gk.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  7. Status Updates:
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.agilealliance.org/glossary/daily-meeting/" rel="noopener noreferrer"&gt;daily stand-up&lt;/a&gt; meeting in &lt;a href="https://www.agilealliance.org/agile101/" rel="noopener noreferrer"&gt;Agile&lt;/a&gt; is an excellent way for the team to discuss the overall progress on their work and any impediments that might be blocking them.&lt;/p&gt;

&lt;p&gt;But once we are done with that, we are literally done with the status updates.&lt;/p&gt;

&lt;p&gt;There was no substantial progress on that &lt;em&gt;"critical"&lt;/em&gt; feature five minutes ago and there won't be any five minutes from now:&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%2F2gupyr8sfmeoehnhyzpj.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%2F2gupyr8sfmeoehnhyzpj.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  8. Showing off Our Work:
&lt;/h3&gt;

&lt;p&gt;We are geeks. Many of us are introverts. Not all of us like announcing our work out loud to the entire world.&lt;/p&gt;

&lt;p&gt;But that doesn't mean we aren't working hard enough or that we aren't worthy of a well-deserved promotion:&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%2F1vzphredx83zbdw64qah.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%2F1vzphredx83zbdw64qah.png" alt="Image Credit: workchronicles.com" width="800" height="1140"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com


 




&lt;h3&gt;
  
  
  9. Getting Our Priorities Right:
&lt;/h3&gt;

&lt;p&gt;Product Managers, please understand this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If all the features are on priority, then none of them are on priority:&lt;/em&gt;&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%2Frl038nxuui89g73vfxbn.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%2Frl038nxuui89g73vfxbn.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com






&lt;h3&gt;
  
  
  10. Meeting (Unrealistic) Expectations:
&lt;/h3&gt;

&lt;p&gt;Need I say more:&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%2F002e8h9uk2cjqx8a9rvk.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%2F002e8h9uk2cjqx8a9rvk.png" alt="Image Credit: workchronicles.com" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
Image Credit: workchronicles.com



&lt;p&gt;(Okay, I admit this one's not specific to programmers, but it nevertheless depicts a chilling reality)&lt;/p&gt;




&lt;p&gt;I hope you folks enjoyed reading this blog!&lt;/p&gt;

&lt;p&gt;Feel free to leave your comments (if any) in the comments section below 👇    &lt;/p&gt;

</description>
      <category>programmerproblems</category>
      <category>programmerlife</category>
      <category>programming</category>
      <category>developers</category>
    </item>
    <item>
      <title>Replace 'em all With ES2021</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Mon, 23 Nov 2020 03:04:03 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/replace-em-all-with-es2021-4e36</link>
      <guid>https://dev.to/pratikgchaudhari/replace-em-all-with-es2021-4e36</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The newly introduced &lt;code&gt;replaceAll()&lt;/code&gt; method removes the need for using a &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;regex&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;If you have ever worked with &lt;code&gt;strings&lt;/code&gt; in JavaScript, you know what a pain it is to replace all the matching substrings in a string!&lt;/p&gt;

&lt;p&gt;Okay, maybe I'm being a little dramatic here, but it sure as hell wasn't something you could do easily as a novice.&lt;/p&gt;

&lt;p&gt;For example, when I first started working with &lt;code&gt;JavaScript&lt;/code&gt; (I was fairly new to programming back then) and wanted to replace all the occurrences of a word in a &lt;code&gt;string&lt;/code&gt;, I just couldn't figure out how to do it by reading the &lt;code&gt;replace()&lt;/code&gt; method's documentation.&lt;/p&gt;

&lt;p&gt;I wondered why I needed to use something so complicated as a &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;regex&lt;/a&gt; to do something so simple as replacing all occurrences of a word in a string (I wasn't a big fan of &lt;code&gt;regex&lt;/code&gt; back then, and to be frank, I still am not).&lt;/p&gt;

&lt;p&gt;Anyway, come &lt;a href="https://tc39.es/ecma262" rel="noopener noreferrer"&gt;ES2021&lt;/a&gt;, you won't need to use a &lt;code&gt;regex&lt;/code&gt; for the said operation!&lt;/p&gt;

&lt;p&gt;That's because &lt;a href="https://tc39.es/ecma262" rel="noopener noreferrer"&gt;ES2021&lt;/a&gt; has introduced a new (and a long-awaited) utility method for replacing all occurrences of a substring in a given string: &lt;code&gt;replaceAll()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let me walk you guys through an example to give you a better idea of what I'm talking about:&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;let&lt;/span&gt; &lt;span class="nx"&gt;aString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I love Java. Java is very very easy to learn. Java's a very popular programming language&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Java&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&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;newString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//I love JavaScript! JavaScript is very very easy to learn. JavaScript's a very popular programming language!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a gif depicting the execution of the above code in Chrome's console (Yeah, Chrome has already shipped this feature, and so have the other browsers):&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%2F02g5cf4lozv7aoptb3gw.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%2F02g5cf4lozv7aoptb3gw.gif" alt="A gif depicting the use of the 'replaceAll()' method in Chrome's console. Image Credits: Pratik Chaudhari" width="648" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
A gif depicting the use of the 'replaceAll()' method in Chrome's console. Image Credits: Pratik Chaudhari



&lt;p&gt;The &lt;code&gt;replaceAll()&lt;/code&gt; method keeps the original string intact and returns a new string.&lt;/p&gt;

&lt;p&gt;Make note here that &lt;code&gt;replaceAll()&lt;/code&gt; makes a case-sensitive comparison while searching for the target substring.&lt;/p&gt;

&lt;p&gt;If I were to replace the uppercase &lt;strong&gt;'J'&lt;/strong&gt; of &lt;strong&gt;'Java'&lt;/strong&gt; with a lowercase &lt;strong&gt;'j'&lt;/strong&gt;, &lt;code&gt;replaceAll()&lt;/code&gt; won't replace it, and the new string will resemble the old one.&lt;/p&gt;

&lt;p&gt;Here's another gif to show you what I mean:&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%2Fcvn5lrcx177ypoontbgf.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%2Fcvn5lrcx177ypoontbgf.gif" alt="A gif depicting the behavior of 'replaceAll()' when we use a lowercase 'j' Image Credits: Pratik Chaudhari" width="648" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
A gif depicting the behavior of 'replaceAll()' when we use a lowercase 'j.' Image Credits: Pratik Chaudhari



&lt;p&gt;Like its cousin &lt;code&gt;replace()&lt;/code&gt;, &lt;code&gt;replaceAll()&lt;/code&gt; also allows you to specify a regex in place of the target substring (i.e., the substring to be replaced), but I think doing so would defeat the entire purpose of using &lt;code&gt;replaceAll()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you'd like to know more about &lt;code&gt;replaceAll()&lt;/code&gt;, feel free to go through its &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; documentation.&lt;/p&gt;

&lt;p&gt;That's all for this article, folks! &lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading this piece and learned something new.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es2021</category>
      <category>string</category>
      <category>stringmanipulation</category>
    </item>
    <item>
      <title>Getting Started With the Github CLI</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sun, 08 Nov 2020 10:38:52 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/getting-started-with-the-github-cli-d55</link>
      <guid>https://dev.to/pratikgchaudhari/getting-started-with-the-github-cli-d55</guid>
      <description>&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;&lt;a href="https://github.com" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, the popular &lt;a href="https://git-scm.com" rel="noopener noreferrer"&gt;Git&lt;/a&gt; Provider has created a command-line tool that allows its users to access &lt;code&gt;GitHub&lt;/code&gt; without leaving their terminals!&lt;/em&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;




&lt;p&gt;In this post, I'll walk you guys through the process of setting up the &lt;a href="https://cli.github.com" rel="noopener noreferrer"&gt;GitHub CLI&lt;/a&gt; on your local machine.&lt;/p&gt;

&lt;p&gt;So, let's start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - Install the GitHub CLI
&lt;/h2&gt;

&lt;p&gt;Before you can start using the GitHub CLI, you must install it on your local machine.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Installing on Mac
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're using &lt;code&gt;macOS,&lt;/code&gt; you can install it using &lt;a href="https://formulae.brew.sh/formula/gh#default" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt;, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;brew&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;gh&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a gif to give you a better idea:&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%2Firpik5mzby19pueas4rt.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%2Firpik5mzby19pueas4rt.gif" alt="Installing the GitHub CLI. Image Credit: Pratik Chaudhari" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;
Installing the GitHub CLI. Image Credit: Pratik Chaudhari



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Installing on Linux
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;For &lt;code&gt;Linux&lt;/code&gt; users, too, &lt;a href="https://formulae.brew.sh/formula/gh#default" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt; should work just fine.&lt;/p&gt;

&lt;p&gt;Although, if you're using a &lt;a href="https://en.wikipedia.org/wiki/Linux_distribution" rel="noopener noreferrer"&gt;Linux distro&lt;/a&gt; that isn't officially supported by &lt;code&gt;GitHub,&lt;/code&gt; you'll have to rely on the community-maintained versions of the CLI.&lt;/p&gt;

&lt;p&gt;Instructions for installing the CLI on such distros can be found &lt;a href="https://github.com/cli/cli/blob/trunk/docs/install_linux.md" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Installing on Windows
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're a &lt;code&gt;Windows&lt;/code&gt; user, you can use &lt;a href="https://chocolatey.org" rel="noopener noreferrer"&gt;Chocolatey&lt;/a&gt; to install the CLI, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;choco&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;gh&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, you can access it using the &lt;code&gt;gh&lt;/code&gt; command, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;gh&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what the above command will output:&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%2Ful5arjauc1tky02nbb19.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%2Ful5arjauc1tky02nbb19.gif" alt="Output after firing the 'gh' command. Image Credit: Pratik Chaudhari." width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;
Output after firing the 'gh' command. Image Credit: Pratik Chaudhari.



&lt;p&gt;Now that the CLI has been installed and is accessible via the &lt;code&gt;Terminal&lt;/code&gt;, let's do something interesting using the CLI, like viewing a &lt;a href="https://guides.github.com/activities/hello-world/" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;gh&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;repo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pratikgchaudhari/vue-3-projects&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command is supposed to display all the info of the &lt;code&gt;vue-3-projects&lt;/code&gt; repo created by its owner &lt;code&gt;pratikgchaudhari&lt;/code&gt;, i.e., me.&lt;/p&gt;

&lt;p&gt;But here's what we get instead: &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%2Fw2tu8zdbditfpoupv0ze.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%2Fw2tu8zdbditfpoupv0ze.gif" alt="Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari." width="866" height="440"&gt;&lt;/a&gt;&lt;/p&gt;
Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.



&lt;p&gt;The &lt;code&gt;GitHub CLI&lt;/code&gt; requires you to authenticate yourself with your GitHub credentials before doing anything else with it!&lt;/p&gt;

&lt;p&gt;And that brings us to our next step! &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 - Authentication
&lt;/h2&gt;

&lt;p&gt;To authenticate yourself, punch in the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;gh&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what you'll 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%2Fndzjbqbpzlr0qnnr3lsc.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%2Fndzjbqbpzlr0qnnr3lsc.png" alt="Output of 'gh auth login' command. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
Output of 'gh auth login' command. Image Credit: Pratik Chaudhari.



&lt;p&gt;The &lt;code&gt;GitHub CLI&lt;/code&gt; is asking you whether you want to sign in using your account on &lt;a href="https://github.com" rel="noopener noreferrer"&gt;GitHub.com&lt;/a&gt;, which is free for all users, or whether you want to use the &lt;a href="https://github.com/enterprise" rel="noopener noreferrer"&gt;GitHub Enterprise&lt;/a&gt; account (most probably owned by your employer or maybe just you).&lt;/p&gt;

&lt;p&gt;Since I don't have an enterprise account, I'll proceed with my &lt;code&gt;GitHub&lt;/code&gt; account. &lt;/p&gt;

&lt;p&gt;Here's what I see after selecting the said option:&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%2Fkb1l4u3cwno5iit5emg6.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%2Fkb1l4u3cwno5iit5emg6.png" alt="Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari.



&lt;p&gt;Since I don't have an &lt;code&gt;authentication token&lt;/code&gt; (yet), I'll choose the &lt;code&gt;Login with a web browser&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;Once I choose the said option, the CLI provides me with a &lt;code&gt;one-time&lt;/code&gt; code:&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%2Ftgzf7k3r37zd4fgsxm9h.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%2Ftgzf7k3r37zd4fgsxm9h.png" alt="Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari.



&lt;p&gt;I copy the &lt;code&gt;one-time&lt;/code&gt; code provided by the CLI and press the &lt;code&gt;Enter/Return&lt;/code&gt; key.&lt;/p&gt;

&lt;p&gt;As expected, a browser window opens where I need to paste the &lt;code&gt;one-time&lt;/code&gt; code that I just copied:&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%2Fhdt87heocx5qjg7h74ye.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%2Fhdt87heocx5qjg7h74ye.png" alt="The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari." width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;
The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari.



&lt;p&gt;After I click the &lt;code&gt;Continue&lt;/code&gt; button, I see the following screen:&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%2Fbotbl321deg03r8v9bzq.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%2Fbotbl321deg03r8v9bzq.png" alt="The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari." width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;
The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari.



&lt;p&gt;Basically, I'm being prompted to authorize the &lt;code&gt;GitHub CLI&lt;/code&gt; to access various resources from my GitHub account.&lt;/p&gt;

&lt;p&gt;So, I'll grant it the access that it requires by clicking on the &lt;code&gt;Authorize github&lt;/code&gt; button. Here's what I see next:&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%2Fr6e22b975gesswvxm242.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%2Fr6e22b975gesswvxm242.png" alt="The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari." width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;
The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari.



&lt;p&gt;The above screen tells me that the authentication process is complete!&lt;/p&gt;

&lt;p&gt;When I switch to my &lt;code&gt;Terminal&lt;/code&gt; and press the &lt;code&gt;Enter/Return&lt;/code&gt; key, the CLI asks me to choose between the following two protocols that it'll use while communicating with the &lt;code&gt;GitHub&lt;/code&gt; server:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HTTPS&lt;/li&gt;
&lt;li&gt;SSH&lt;/li&gt;
&lt;/ol&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%2F6tczlktiqkjvzq70rn59.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%2F6tczlktiqkjvzq70rn59.png" alt="The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari.



&lt;p&gt;I'll choose the &lt;code&gt;SSH&lt;/code&gt; protocol here, but there's no harm in selecting the &lt;code&gt;HTTPS&lt;/code&gt; protocol either:&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%2Fzwvcay4a2dnnxlkxxq9i.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%2Fzwvcay4a2dnnxlkxxq9i.png" alt="Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari.



&lt;p&gt;And it's done!&lt;/p&gt;

&lt;p&gt;Let's execute the following command again and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;gh&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;repo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pratikgchaudhari/vue-3-projects&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F5qjqr99hhp1esta5bq5z.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%2F5qjqr99hhp1esta5bq5z.png" alt="Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari." width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.



&lt;p&gt;As you can see in the above screenshot, the title of the repository along with the contents of its &lt;code&gt;README&lt;/code&gt; are displayed on the &lt;code&gt;Terminal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;GitHub CLI&lt;/code&gt; provides many more commands which I'll be covering in detail in a separate article in the future.&lt;/p&gt;

&lt;p&gt;For now, this is it!&lt;/p&gt;

</description>
      <category>github</category>
      <category>githubcli</category>
      <category>cli</category>
      <category>commandline</category>
    </item>
    <item>
      <title>Meet Deno: The New JavaScript/TypeScript Runtime</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sun, 01 Nov 2020 18:18:39 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/meet-deno-the-new-javascript-typescript-runtime-49m2</link>
      <guid>https://dev.to/pratikgchaudhari/meet-deno-the-new-javascript-typescript-runtime-49m2</guid>
      <description>&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%2Ffmwmtv0tp1e4m8f2iw0t.jpeg" 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%2Ffmwmtv0tp1e4m8f2iw0t.jpeg" alt="Photo by Frank P. via Pixabay - A picture of the long-necked Brachiosaurus" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;
 
Photo by &lt;a href="https://pixabay.com/users/frank_p_ajj74-1589101" rel="noopener noreferrer"&gt; Frank P.&lt;/a&gt; via 
&lt;a href="https://pixabay.com/photos/dinosaur-prehistoric-times-lizards-4475295" rel="noopener noreferrer"&gt; Pixabay&lt;/a&gt;




&lt;blockquote&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Deno 1.0 was released on May 13, 2020&lt;/em&gt;
&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://deno.land/" rel="noopener noreferrer"&gt;Deno&lt;/a&gt; is &lt;a href="https://github.com/ry" rel="noopener noreferrer"&gt;Ryan Dahl's&lt;/a&gt; (yeah, you guessed it right, the guy who created &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt;) latest venture.&lt;/p&gt;

&lt;p&gt;But it isn't just another &lt;a href="https://en.wikipedia.org/wiki/JavaScript_engine" rel="noopener noreferrer"&gt;JavaScript Engine&lt;/a&gt;. It also supports &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt; - JavaScript's strictly typed cousin - out of the box.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Deno
&lt;/h2&gt;

&lt;p&gt;On &lt;code&gt;macOS&lt;/code&gt;, you can install Deno using &lt;a href="https://formulae.brew.sh/formula/deno#default" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt; - the open-source software package manager for &lt;code&gt;macOS&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;brew&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;deno&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a GIF to give you a better idea:&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%2Fl6bkymfik9rlq7xqnltg.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%2Fl6bkymfik9rlq7xqnltg.gif" alt="The output of the " width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

The output of the "brew install deno" command. 
Image Credit: Pratik Chaudhari (Author)




&lt;p&gt;On &lt;code&gt;Windows&lt;/code&gt;, &lt;a href="https://chocolatey.org/" rel="noopener noreferrer"&gt;Chocolately&lt;/a&gt; serves as an alternative to macOS's Homebrew:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;choco&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;deno&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On &lt;code&gt;Linux&lt;/code&gt;, good ol' curl will do the job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;curl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-fsSL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;https://deno.land/x/install/install.sh&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sh&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once &lt;code&gt;Deno&lt;/code&gt; is installed, one can run the following command to see all the command-line options that are available:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;deno&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--help&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above command will look something like below:&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%2Fi7g5ktpegmb2to7qjz5n.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%2Fi7g5ktpegmb2to7qjz5n.png" alt="The output of executing the " width="800" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

The output of executing the "deno" command on Terminal. Image Credit: Pratik Chaudhari (Author)




&lt;h2&gt;
  
  
  Hello, Deno!
&lt;/h2&gt;

&lt;p&gt;You can run &lt;code&gt;Deno&lt;/code&gt; in REPL (Read-Eval-Print Loop) mode by simply executing the following command in the Terminal/Powershell/Shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;deno&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the REPL is active, type the following code on the prompt and press the Enter/Return key:&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="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;Hello, Deno!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what you'll 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%2Fm8sccxjvyydo28lhtkdy.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%2Fm8sccxjvyydo28lhtkdy.png" alt="The output of console.log('Hello, Deno!'). Image Credit: Pratik Chaudhari (Author)" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

The output of console.log('Hello, Deno!'). Image Credit: Pratik Chaudhari (Author)




&lt;p&gt;This is all good and fine. But it's just like all other "Hello World!" programs out there!&lt;/p&gt;

&lt;p&gt;Let's have some real fun.&lt;/p&gt;

&lt;p&gt;Let's see if we can build a web server in &lt;code&gt;Deno&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using third-party/external packages
&lt;/h2&gt;

&lt;p&gt;In Node.js, if you want to (or rather, need to) use a third-party library in your code, you first need to install it using &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;npm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then you import it into your code using this:&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;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;Deno&lt;/code&gt; allows you to import such packages directly. You just need to specify the package's URL in your code. Let's see how:&lt;/p&gt;

&lt;p&gt;Fire up the &lt;a href="https://www.nano-editor.org/" rel="noopener noreferrer"&gt;nano&lt;/a&gt; editor and add the following code to it:&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;serve&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="s2"&gt;https://deno.land/std@0.63.0/http/server.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8000&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="s2"&gt;http://localhost:8000/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &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;req&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save this in a file named &lt;code&gt;server.ts&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%2Fts8suaeyqpngemztvbcj.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%2Fts8suaeyqpngemztvbcj.png" alt="The code to create a web server in the nano editor. Image Credit: Pratik Chaudhari (Author)" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

The code to create a web server in the nano editor. Image Credit: Pratik Chaudhari (Author)




&lt;p&gt;Exit the nano editor or open a new terminal and execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;deno&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;server.ts&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above command, the server should start listening over port &lt;code&gt;8000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But what we get instead is 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%2Fi%2Fyqh0gnmm45o6lnwiij54.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%2Fyqh0gnmm45o6lnwiij54.png" alt="Error after running the " width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

Error after running the "deno run server.ts" command. Image Credit: Pratik Chaudhari.




&lt;p&gt;What does the following error in the above screenshot mean?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;error:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Uncaught&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;PermissionDenied:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.0.0.0:8000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;again&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;with&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--allow-net&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see, you need to explicitly grant &lt;code&gt;Deno&lt;/code&gt; the permission to access the network. This is one of the many &lt;a href="https://deno.land/manual/getting_started/permissions" rel="noopener noreferrer"&gt;security features&lt;/a&gt; that are built into &lt;code&gt;Deno&lt;/code&gt; (that's why they call it a &lt;em&gt;&lt;strong&gt;secure&lt;/strong&gt;&lt;/em&gt; runtime).&lt;/p&gt;

&lt;p&gt;You can allow &lt;code&gt;Deno&lt;/code&gt; to access the network by specifying the command line flag &lt;code&gt;--allow-net&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;deno&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--allow-net&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;server.ts&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila! The server starts listening for requests on port &lt;code&gt;8000&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%2Fyfts7zdbh6fk6zkvku39.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%2Fyfts7zdbh6fk6zkvku39.png" alt="The web server running on port 8000. Image Credit: Pratik Chaudhari (Author)" width="742" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

The web server running on port 8000. Image Credit: Pratik Chaudhari (Author)




&lt;p&gt;If you look at the following line of code, you'll notice that we have specified the full URL of the &lt;code&gt;server.ts&lt;/code&gt; file which contains the &lt;code&gt;serve()&lt;/code&gt; function that we use in our code to listen &amp;amp; serve requests on port &lt;code&gt;8000&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;serve&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="s2"&gt;https://deno.land/std@0.63.0/http/server.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We didn't have to install it using &lt;code&gt;npm&lt;/code&gt; beforehand. &lt;code&gt;Deno&lt;/code&gt; automatically downloads it while executing the above code. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Deno&lt;/code&gt; also downloads any other packages that might be required by &lt;code&gt;server.ts&lt;/code&gt; i.e., it also takes care of the &lt;em&gt;&lt;strong&gt;transitive dependencies&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This saves us the trouble of executing &lt;code&gt;npm install&lt;/code&gt; before running our programs.&lt;/p&gt;

&lt;p&gt;This is something that's new and unique to &lt;code&gt;Deno&lt;/code&gt; (apart from the need for explicit permissions, that we saw before) and makes it stand apart from &lt;code&gt;Node.js&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;So far, &lt;code&gt;Deno&lt;/code&gt; seems to be a good alternative to &lt;code&gt;Node.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But will it be able to take its place someday?&lt;/p&gt;

&lt;p&gt;Only time will tell.&lt;/p&gt;

</description>
      <category>deno</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Vue.js 3: First Steps</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sun, 01 Nov 2020 14:52:17 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/vue-js-3-first-steps-27c2</link>
      <guid>https://dev.to/pratikgchaudhari/vue-js-3-first-steps-27c2</guid>
      <description>&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%2Fidt01ch8uowzh5q7ubl2.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%2Fidt01ch8uowzh5q7ubl2.png" alt="Vue.js 3.0. Image Credits: JavaScript Weekly" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;
Vue.js 3.0. Image Credits: &lt;a href="https://javascriptweekly.com/issues/506" rel="noopener noreferrer"&gt;JavaScript Weekly&lt;/a&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Code-named "One Piece," Vue 3.0 is the latest version of one of the most popular front-end frameworks.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Sept 18th, 2020, came bearing the good news of &lt;a href="http://v3.vuejs.org/" rel="noopener noreferrer"&gt;Vue 3.0's&lt;/a&gt; release.&lt;/p&gt;

&lt;p&gt;The Vue.js team announced its release via its official Twitter account:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1306992969728380930-594" src="https://platform.twitter.com/embed/Tweet.html?id=1306992969728380930"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1306992969728380930-594');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1306992969728380930&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;I was excited to try it out and decided to create a small Todo app.&lt;/p&gt;

&lt;p&gt;In this article, I'll walk you guys through the process of creating a web app using Vue 3.0.&lt;/p&gt;

&lt;p&gt;But before we start, let me give you a glimpse of what we are about to create:&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%2Fdbhu2s9vmitooqzna102.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%2Fdbhu2s9vmitooqzna102.gif" alt="The Todo app created using Vue 3.0. Image Credits: Pratik Chaudhari (Author)" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;
The Todo app created using Vue 3.0. Image Credits: Pratik Chaudhari (Author)



&lt;p&gt;Now that you have got a visual clue of how it's going to look and how it's going to function, let's dive into the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting up your app to use Vue 3.0
&lt;/h3&gt;

&lt;p&gt;We won't be using &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, the popular JavaScript package manager, to keep things simple.&lt;/p&gt;

&lt;p&gt;Instead, we'll use the good ol' &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag to import vue.js into our app directly from a CDN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/vue@next"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
```



Note the _**@next**_ in the above code. It is used to indicate to [unpkg.com](https://unpkg.com/) that we want the latest version of vue.js.

If you omit the _**@next**_, [unpkg.com](https://unpkg.com/) will provide you with vue 2.6.12, the current [LTS](https://en.wikipedia.org/wiki/Long-term_support) version of Vue.js.

## Step 2.

### Importing other third-party libraries

The only third-party library that I have used is the [Materialize CSS](https://materializecss.com/).

This isn't really necessary, but it gives the app a nice swanky look :)

So let's import it:



```html
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
```



As you can see, we need two imports here:

1. The Materialize CSS file
2. The Materialize JS file

The JS file is required for displaying toasters and tooltips provided by the Materialize CSS.

## Step 3.

### Creating the HTML template

We'll need a form with two input fields: one for the title and one for the description. We'll also need a button that will allow the user to submit a todo item to the app.

Here's the code:



```html
&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"col s12"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"row"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"input-field col s4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"material-icons prefix"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;title&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"todo.title"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo_title"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"materialize-textarea"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"todo_title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;A small title would be nice...&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"input-field col s4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"material-icons prefix"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;description&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"todo.description"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo_description"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"materialize-textarea"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"todo_description"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Maybe add a little description...&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"s4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn-floating btn-large waves-effect waves-light red tooltipped"&lt;/span&gt; &lt;span class="na"&gt;data-position=&lt;/span&gt;&lt;span class="s"&gt;"bottom"&lt;/span&gt;
                &lt;span class="na"&gt;data-tooltip=&lt;/span&gt;&lt;span class="s"&gt;"Add Todo!"&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"addTodo()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"material-icons"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;add&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
```



We'll also need a table to display all the todos that the user has added.

Here's how we design it:



```html
&lt;span class="nt"&gt;&amp;lt;table&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"striped centered responsive-table"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;
            Title
        &lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;
            Description
        &lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;tr&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"(todo,id) in todos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
                {{id}}
            &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
                {{todo.title}}
            &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
                {{todo.description}}
            &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; 
                   &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"!todo.done"&lt;/span&gt;
                   &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn-floating btn-large waves-effect 
                   waves-light light-green accent-4 tooltipped"&lt;/span&gt;
                   &lt;span class="na"&gt;data-position=&lt;/span&gt;&lt;span class="s"&gt;"bottom"&lt;/span&gt; 
                   &lt;span class="na"&gt;data-tooltip=&lt;/span&gt;&lt;span class="s"&gt;"I'm done with this one!"&lt;/span&gt; 
                   &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"markAsDone(todo.id)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"material-icons"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;check&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;

                &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"todo.done"&lt;/span&gt; 
                   &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"todo-done-tick material-icons 
                   tooltipped"&lt;/span&gt; 
                   &lt;span class="na"&gt;data-position=&lt;/span&gt;&lt;span class="s"&gt;"bottom"&lt;/span&gt;
                   &lt;span class="na"&gt;data-tooltip=&lt;/span&gt;&lt;span class="s"&gt;"You're done with this 
                   one!"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                 check
               &lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;tr&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"todoCount == 0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;td&lt;/span&gt; &lt;span class="na"&gt;colspan=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
              You haven't added any Todos yet :(
            &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
```



## Step 4.

### Creating the app.js

Let's create the app.js, which will bring our app to life:



```javascript
const TodoApp = {
    data() {
        return {
            todoCount: 0,
            todo: {
                id: 0,
                title: '',
                description: '',
                done: false
            },
            todos: {}
        }
    },
    mounted() {
        const vm = this;
        vm.initialize();
    },
    methods: {
        initialize() {
            const vm = this;
            vm.addTooltips(vm.findTooltippedElementsFromDOM());
        },
        addTooltips(tooltippedElements) {
            const vm = this;
            M.Tooltip.init(tooltippedElements, {});
        },
        findTooltippedElementsFromDOM() {
            const vm = this;
            return document.querySelectorAll('.tooltipped');
        },
        addTodo() {
            const vm = this;

            if (!vm.todo.title || vm.todo.title.trim() === '') {
                M.toast({ html: 'Need a title for this Todo!', classes: 'rounded' });
                return;
            }

            if (!vm.todo.description || vm.todo.description.trim() === '') {
                M.toast({ html: 'A small description would be nice!', classes: 'rounded' });
                return;
            }

            vm.todo.id = ++vm.todoCount;
            vm.todos[vm.todo.id] = vm.todo;

            vm.todo = { title: '', description: '' };
            vm.addTooltipsToDynamicElements();
        },
        markAsDone(id) {
            const vm = this;
            vm.todos[id].done = true;
            vm.addTooltipsToDynamicElements();
        },
        addTooltipsToDynamicElements() {
            const vm = this;

            setTimeout(function () {
                vm.addTooltips(vm.findTooltippedElementsFromDOM());
            }, 500);
        }
    }
}

Vue.createApp(TodoApp).mount('#todoapp');
```



Once we glue all the above code together, our app will begin to function, as we'd seen in the gif at the beginning of the article.

In case you want to see a live demo of this app (and maybe change its code and play with it), head over to the Codepen below, where I've uploaded all the code:

&lt;iframe height="600" src="https://codepen.io/pratikgchaudhari/embed/JjKvRgx?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;


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

&lt;/div&gt;

</description>
      <category>vue3</category>
      <category>vue</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Bonjour V!</title>
      <dc:creator>Pratik Chaudhari</dc:creator>
      <pubDate>Sun, 01 Mar 2020 10:57:19 +0000</pubDate>
      <link>https://dev.to/pratikgchaudhari/bonjour-v-9e6</link>
      <guid>https://dev.to/pratikgchaudhari/bonjour-v-9e6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Let's meet the new kid on the programming block.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://vlang.io/" rel="noopener noreferrer"&gt;V is a brand new programming language&lt;/a&gt; that's been making quite a few waves in the programming community.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I stumbled upon it a few months back when I read &lt;a href="https://hub.packtpub.com/the-v-programming-language-is-now-open-sourced-is-it-too-good-to-be-true/" rel="noopener noreferrer"&gt;this&lt;/a&gt; article.&lt;/p&gt;

&lt;p&gt;While V is still in its infancy as of this writing (there are only &lt;strong&gt;&lt;em&gt;&lt;a href="https://stackoverflow.com/questions/tagged/vlang" rel="noopener noreferrer"&gt;six&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; questions about it on &lt;a href="https://stackoverflow.com/" rel="noopener noreferrer"&gt;SO&lt;/a&gt;), it is gaining popularity (it has around &lt;a href="https://github.com/vlang/v/stargazers" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;15.9K&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; stars on GitHub).&lt;/p&gt;

&lt;p&gt;And it's also under active development (as evidenced by the recent commits in its &lt;a href="https://github.com/vlang/v" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So, I thought, why not give it a try. After all, what's the harm in being an early adopter, right?&lt;/p&gt;

&lt;p&gt;So, let's get started!&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1. Install V on your local machine
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;You can install V using prebuilt binaries (such as &lt;a href="https://github.com/vlang/v/releases/latest/download/v_macos.zip" rel="noopener noreferrer"&gt;this one&lt;/a&gt;), or you can directly &lt;a href="https://github.com/vlang/v#installing-v-from-source" rel="noopener noreferrer"&gt;compile&lt;/a&gt; its &lt;a href="https://github.com/vlang/v" rel="noopener noreferrer"&gt;source code&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since I use a &lt;em&gt;MacBook Air (13-inch, 2007) running &lt;a href="https://www.apple.com/in/macos/catalina/" rel="noopener noreferrer"&gt;macOS Catalina&lt;/a&gt; (version 10.15.3),&lt;/em&gt; I downloaded &lt;a href="https://github.com/vlang/v/releases/latest/download/v_macos.zip" rel="noopener noreferrer"&gt;this&lt;/a&gt; prebuilt binary for macOS.&lt;/p&gt;

&lt;p&gt;I extracted its contents and fired up the &lt;a href="https://support.apple.com/en-in/guide/terminal/welcome/mac" rel="noopener noreferrer"&gt;&lt;em&gt;macOS Terminal&lt;/em&gt;&lt;/a&gt;. Then, I navigated to the directory where I'd extracted the prebuilt binary and fired the following command in the Terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;&lt;code&gt;./v version&lt;/code&gt;&lt;/em&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's supposed to display V's current version, something like below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;&lt;code&gt;V 0.1.24 0d93eeb&lt;/code&gt;&lt;/em&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;But instead, the following prompt popped up on the screen:&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%2Fobm7zq45kyjalvsw3pp9.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%2Fobm7zq45kyjalvsw3pp9.png" alt="A warning prompt from macOS stating that macOS is unable to verify app (https://support.apple.com/en-us/HT202491)" width="532" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see, macOS blocks the execution of apps that haven't been &lt;em&gt;&lt;a href="https://developer.apple.com/documentation/xcode/notarizing_macos_software_before_distribution" rel="noopener noreferrer"&gt;notarized&lt;/a&gt; (meaning, apple can't verify the origins of the app, and as a result, can't guarantee that the application isn't malicious).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, what do we do now? Give up?&lt;/p&gt;

&lt;p&gt;Absolutely not!&lt;/p&gt;

&lt;p&gt;There's a workaround: We can tell macOS to make an exception for such &lt;em&gt;unnotarized&lt;/em&gt; apps by navigating to &lt;em&gt;System Preferences &amp;gt; Security &amp;amp; Privacy&lt;/em&gt; and click the "Allow Anyway" button. Take a peek at the &lt;em&gt;gif&lt;/em&gt; below to get a better idea:&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%2Fk2obmpduwvk2z49nbajg.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%2Fk2obmpduwvk2z49nbajg.gif" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we've told the bouncer (macOS 😀) to let the guest in, we should be able to start the party, right? (run V via the Terminal without any issue)&lt;/p&gt;

&lt;p&gt;But no! 😯 That's not what happens. When we execute the &lt;code&gt;./v version&lt;/code&gt; on the Terminal again, the following screen pops up:&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%2Fru1t4h1pcm8bfddnwful.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%2Fru1t4h1pcm8bfddnwful.png" alt="macOS blocks the execution of the app yet again, but this time, there's an " width="581" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the same pop-up that we encountered a while back, but this time, macOS has provided us with the "Open" button. Let's click it and see what happens:&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%2Ffh4w41bcywj3ormqe61v.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%2Ffh4w41bcywj3ormqe61v.png" alt="V prints its version number on the Terminal" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As soon as we click "Open", the &lt;code&gt;./v version&lt;/code&gt; command executes and prints its output on the Terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2. Start writing code in V
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Now that V's compiler is up &amp;amp; running on our local machine, we can start writing programs in V.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But wait! Don't we need a fancy IDE like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;&lt;em&gt;VSCode&lt;/em&gt;&lt;/a&gt;, with all its elegant themes and plugins 😄 (just joking, it's my favorite IDE.)&lt;/p&gt;

&lt;p&gt;No, not yet, at least!&lt;/p&gt;

&lt;p&gt;V provides a &lt;a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="noopener noreferrer"&gt;&lt;em&gt;REPL&lt;/em&gt;&lt;/a&gt; interface that allows you to play around with the language without having to create any files on your hard disk.&lt;/p&gt;

&lt;p&gt;So, as the &lt;a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" rel="noopener noreferrer"&gt;&lt;em&gt;Hello World Tradition&lt;/em&gt;&lt;/a&gt; dictates, let's write our first program in V:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;print("Bonjour V!")&lt;/code&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;Press the "enter/return" key on your keyboard and let the magic begin!&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%2Fzpwjblj1nnsuzmyzm43q.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%2Fzpwjblj1nnsuzmyzm43q.png" alt="The traditional " width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! It worked!&lt;/p&gt;

&lt;p&gt;That's it, guys!&lt;/p&gt;

&lt;p&gt;Stay tuned for more stories about V programming!&lt;/p&gt;

</description>
      <category>v</category>
      <category>vprogramming</category>
      <category>helloworld</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
