<?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: Sagar Rout</title>
    <description>The latest articles on DEV Community by Sagar Rout (@sagarrth).</description>
    <link>https://dev.to/sagarrth</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%2F188972%2F9348b64b-7bd6-4f0d-a82e-6651d6061a30.jpeg</url>
      <title>DEV Community: Sagar Rout</title>
      <link>https://dev.to/sagarrth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagarrth"/>
    <language>en</language>
    <item>
      <title>Never again, be stumped by JS Abstract Equality 🤔</title>
      <dc:creator>Sagar Rout</dc:creator>
      <pubDate>Sun, 07 Feb 2021 12:36:57 +0000</pubDate>
      <link>https://dev.to/sagarrth/never-again-be-stumped-by-js-abstract-equality-2ahh</link>
      <guid>https://dev.to/sagarrth/never-again-be-stumped-by-js-abstract-equality-2ahh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;JavaScript has Strict Equality a.k.a &lt;code&gt;===&lt;/code&gt; comparison and Abstract Equality a.k.a &lt;code&gt;==&lt;/code&gt; comparison. It is the latter's lack of understanding which might cause bugs.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Contents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ToPrimitive, ValueOf, ToString&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementing ToPrimitive&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Rules &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/w78T5aWrM7eLatdwDE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/w78T5aWrM7eLatdwDE/giphy.gif" alt="rule" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the rules which come into effect while resolving the abstract equality -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;x=null&lt;/code&gt; and &lt;code&gt;y=undefined&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;x=undefined&lt;/code&gt; and &lt;code&gt;y=null&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)&lt;/code&gt; is same as &lt;code&gt;Type(y)&lt;/code&gt; then perform strict equality comparison.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)=Number&lt;/code&gt; and &lt;code&gt;Type(y)=String&lt;/code&gt;, return result of &lt;code&gt;x==ToNumber(y)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)=String&lt;/code&gt; and &lt;code&gt;Type(y)=Number&lt;/code&gt;, return result of &lt;code&gt;ToNumber(x)==y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)=Boolean&lt;/code&gt;, return result of &lt;code&gt;ToNumber(x)==y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(y)=Boolean&lt;/code&gt;, return result of &lt;code&gt;x==ToNumber(y)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)&lt;/code&gt; is either &lt;code&gt;String, Number or Symbol&lt;/code&gt; &amp;amp; &lt;code&gt;Type(y)&lt;/code&gt; is &lt;code&gt;Object&lt;/code&gt;, then return result of &lt;code&gt;x==ToPrimitive(y)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(y)&lt;/code&gt; is either &lt;code&gt;String, Number or Symbol&lt;/code&gt; &amp;amp; &lt;code&gt;Type(x)&lt;/code&gt; is &lt;code&gt;Object&lt;/code&gt;, then return result of &lt;code&gt;ToPrimitive(x)==y&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h6&gt;
  
  
  Type, ToNumber, ToBoolean are all abstract operations, they are implemented by the JS engine
&lt;/h6&gt;




&lt;h3&gt;
  
  
  ToPrimitive, ValueOf, ToString &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;8th and 9th cases are quite interesting. It involves the conversion of a non-primitive value to a primitive one.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison"&gt;spec&lt;/a&gt;, the &lt;code&gt;ToPrimitive&lt;/code&gt; method accepts two arguments - &lt;em&gt;Input&lt;/em&gt; and optional &lt;em&gt;PreferredType&lt;/em&gt; hint (it could be either &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt; or &lt;code&gt;default&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;object&lt;/code&gt; utilizes the following two methods for conversion if the &lt;code&gt;ToPrimitive&lt;/code&gt; is not available for the &lt;code&gt;object&lt;/code&gt; -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;valueOf&lt;/code&gt; - takes precedence in case of &lt;code&gt;number&lt;/code&gt; hint i.e during numerical comparisons, followed by a call to &lt;code&gt;toString&lt;/code&gt;, if the former does not yield a primitive value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;toString&lt;/code&gt; - takes precedence in case of &lt;code&gt;string&lt;/code&gt; hint, followed by a call to &lt;code&gt;valueOf&lt;/code&gt;, if the former does not yield a primitive value&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the &lt;code&gt;default&lt;/code&gt; hint case, it's considered as &lt;code&gt;number&lt;/code&gt; unless the &lt;code&gt;object&lt;/code&gt; is of &lt;code&gt;Date&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;Enough talking, let's see code in action for the above statements&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@sagarrth/Abstract-Equality-and-Coercion?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;





&lt;h3&gt;
  
  
  Implementing ToPrimitive &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Symbol.toPrimitive&lt;/code&gt; is the way to implement an &lt;code&gt;object&lt;/code&gt;'s &lt;code&gt;toPrimitive&lt;/code&gt; operation. If this method does not return a primitive, then an error is thrown. The snippet below shows how one could implement any custom logic for object coercion.&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@sagarrth/Implementing-ToPrimitive?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;





&lt;p&gt;Reference: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://262.ecma-international.org/11.0/#sec-toprimitive"&gt;ECMAScript spec 7.1.1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison"&gt;ECMAScript spec 7.2.15&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://262.ecma-international.org/11.0/#sec-well-known-symbols"&gt;ECMAScript spec 6.1.5.1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all people!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/26gsjCZpPolPr3sBy/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26gsjCZpPolPr3sBy/giphy.gif" alt="thankyou" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codepen</category>
      <category>uiweekly</category>
    </item>
    <item>
      <title>🚀 Demystifying Async/Await as Generators + Promises</title>
      <dc:creator>Sagar Rout</dc:creator>
      <pubDate>Sun, 07 Feb 2021 00:00:45 +0000</pubDate>
      <link>https://dev.to/sagarrth/demystifying-async-await-as-generators-promises-91i</link>
      <guid>https://dev.to/sagarrth/demystifying-async-await-as-generators-promises-91i</guid>
      <description>&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Async/Await solves the problem of Inversion of Control with callbacks.&lt;/li&gt;
&lt;li&gt;Generators build-up to Async/Await&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Contents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Introduction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Async/Await&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generators&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Async/Await through generators&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;







&lt;h3&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript being single-threaded, should not be blocked for long-running tasks.&lt;br&gt;
Callbacks are the answer to execute such tasks without blocking the main thread. However, they cause &lt;strong&gt;Inversion of Control&lt;/strong&gt;. &lt;em&gt;The invocation of callbacks is passed to a function over which you do not have control&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Async/Await solves that problem. In this article, we would compare the same example and break down the similar working of the async/await function with the help of generators.&lt;/p&gt;




&lt;h3&gt;
  
  
  Async/Await &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Async functions help us write asynchronous code (Promises) in a more synchronous manner.&lt;/p&gt;

&lt;p&gt;Things to note about async functions - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Async functions always return a Promise, returned values are enclosed in a promise if they are not thenables.&lt;/li&gt;
&lt;li&gt;Await keyword can only be used within an async function, it is used to await the value of a promise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1fs0iv0xs534yb3ijn77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1fs0iv0xs534yb3ijn77.png" alt="async await example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above snippet, the invocation of the async function returns us the data, without having to do a .then over the two promises returned from the fetch API and its parsing process.&lt;/p&gt;




&lt;h3&gt;
  
  
  Generators &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Generators are functions that do not run to completion. They can be paused and resumed. Until the function is completed, its context is preserved. It returns a generator object which has the information about the function context. It can be inspected by logging an execution of a simple generator function. This helps us write asynchronous code in a somewhat synchronous manner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm9msjztir2rmmqt3l70a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm9msjztir2rmmqt3l70a.png" alt="generator example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The generator object is compatible with the Iterable protocol, thus a .next() can be invoked to get subsequent values i.e. control goes back within the generator function.&lt;/li&gt;
&lt;li&gt;Values are exchanged between the generator function and outer code via next/yield. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feoo7u60zohpsgw2s3yum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feoo7u60zohpsgw2s3yum.png" alt="Iterator interface"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Async/Await through generators &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Understanding the generators is essential for the last part of the article. It is where Promises and Generators will be composed to create our own async/await implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/VFHWGT96hbfsjDJ57S/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/VFHWGT96hbfsjDJ57S/giphy.gif" alt="almost there"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code snippet is analogous to the async/await code snippet at the top. Here is the breakdown of the steps -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A generator function is required for it, yield is analogous to the await keyword.&lt;/li&gt;
&lt;li&gt;The asyncify function returns a Promise, which embodies the await logic.&lt;/li&gt;
&lt;li&gt;The manual invocation of the iterator.next() method which was seen in the generator's example is done within the promise body.&lt;/li&gt;
&lt;li&gt;The logic would be recursively invoked until the IteratorResult has 'done' as true &lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Ultimately, the &lt;code&gt;asyncified&lt;/code&gt; function would be consumed in a similar manner as an async function is. As it returns a Promise, it should be consumed via a success and error handler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzhgabsie3dq65jjftnd0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzhgabsie3dq65jjftnd0.png" alt="asyncified consumption"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;All above snippets can be run &lt;a href="https://codepen.io/sagarrth/pen/PobNpwZ" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;br&gt;
Reference: &lt;a href="https://exploringjs.com/es6/ch_generators.html" rel="noopener noreferrer"&gt;ExploringJS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all people!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>uiweekly</category>
      <category>generators</category>
    </item>
    <item>
      <title>An overview of Event Loop, Tasks, and Microtasks</title>
      <dc:creator>Sagar Rout</dc:creator>
      <pubDate>Thu, 08 Aug 2019 12:27:35 +0000</pubDate>
      <link>https://dev.to/sagarrth/an-overview-of-event-loop-tasks-and-microtasks-1i31</link>
      <guid>https://dev.to/sagarrth/an-overview-of-event-loop-tasks-and-microtasks-1i31</guid>
      <description>&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;The browser uses an event loop to effectively switch between scripting, rendering, painting, networking, eventing, and similar other jobs.&lt;/li&gt;
&lt;li&gt;Although JS is single-threaded, concurrency is possible with help of an Event loop and a bunch of Web APIs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Task (Macro task)?
&lt;/h2&gt;

&lt;p&gt;The task is code to be executed until completion. For each turn of the event loop, one task is executed. A task can schedule other tasks (asynchronous in nature). Multiple task queues are maintained by the browser.&lt;/p&gt;

&lt;p&gt;Task sources are - DOM Manipulation, UI Events, History Traversal, Networking&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/macrotasks?embed=1&amp;amp;&amp;amp;" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Taking into account the use of &lt;code&gt;setTimeout&lt;/code&gt;, it helps us defer code execution. After resetting of timer for each &lt;code&gt;setTimeout&lt;/code&gt; method, the callback function will be pushed to the tasks queue for processing. Each timer runs separately from the main thread. This way it does not block the main thread.&lt;/p&gt;

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

&lt;p&gt;This is code that needs to be executed after the currently executing task is completed.&lt;/p&gt;

&lt;p&gt;Tasks (Macro, Microtasks) can schedule more tasks and they get added to their respective queues. Microtasks are &lt;strong&gt;kind of blocking&lt;/strong&gt; in nature. Unlike macro tasks, the main thread will be blocked until the microtask queue is empty. All of it will be processed in the same turn of the event loop&lt;/p&gt;

&lt;p&gt;Microtask sources are - Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/microtasks?embed=1&amp;amp;&amp;amp;" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The blocking nature of microtasks can be exhibited in the above code by increasing the iterations in for loop to a large value.&lt;/p&gt;

&lt;p&gt;The microtasks queue is processed before the next rendering and painting jobs. If they are long-running then it will lead to visual degradation.&lt;/p&gt;

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

&lt;p&gt;The summarised version of the event loop algorithm -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process the oldest task from the tasks queue&lt;/li&gt;
&lt;li&gt;If there is a microtasks queue, process all entries until its emptied&lt;/li&gt;
&lt;li&gt;Do rendering, painting, and so forth&lt;/li&gt;
&lt;li&gt;Repeat the above steps if the tasks queue is not empty, otherwise wait&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://html.spec.whatwg.org/multipage/webappapis.html#event-loops"&gt;Spec&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;t=17s"&gt;Phillip Robert's awesome talk&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>eventloop</category>
      <category>codepen</category>
    </item>
  </channel>
</rss>
