<?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: Nishant Kumar Jha</title>
    <description>The latest articles on DEV Community by Nishant Kumar Jha (@nkj).</description>
    <link>https://dev.to/nkj</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%2F919909%2F07a84957-42f3-427e-8e6a-fa66301fac2f.jpg</url>
      <title>DEV Community: Nishant Kumar Jha</title>
      <link>https://dev.to/nkj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nkj"/>
    <language>en</language>
    <item>
      <title>Sync/Async using the fs module in Node JS</title>
      <dc:creator>Nishant Kumar Jha</dc:creator>
      <pubDate>Tue, 06 Sep 2022 20:36:05 +0000</pubDate>
      <link>https://dev.to/nkj/syncasync-using-the-fs-module-in-node-js-mcj</link>
      <guid>https://dev.to/nkj/syncasync-using-the-fs-module-in-node-js-mcj</guid>
      <description>&lt;p&gt;On the journey of learning backend development with Node, I will be sharing my learnings each day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Using fs module Synchronously:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CzAqX64r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyslpnw67glp79urkciv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CzAqX64r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyslpnw67glp79urkciv.png" alt="Synchronous fs module" width="880" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JwgRyv4Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1cadh05p1v9k7d35ju7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JwgRyv4Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1cadh05p1v9k7d35ju7.png" alt="Sync result" width="428" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using fs module Asynchronously:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GsyvXxds--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n22c25rzzdjf6mg0jtbz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GsyvXxds--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n22c25rzzdjf6mg0jtbz.png" alt="Asynchronous fs module" width="880" height="698"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1m86C9DE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/719c86u9tof011yu9yw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1m86C9DE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/719c86u9tof011yu9yw1.png" alt="Async result" width="490" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronous&lt;/strong&gt; is the vanilla JavaScript way of executing code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the fs module is implemented using &lt;strong&gt;sync&lt;/strong&gt;, the code execution takes place line-by-line and does not move to the next line until the current is executed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus, the console logs written in the sync code are executed in the same sequence as they are written.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whereas, when fs is implemented using &lt;strong&gt;async&lt;/strong&gt;, we can notice the line &lt;code&gt;starting next task&lt;/code&gt; is printed before &lt;code&gt;done with this task&lt;/code&gt;, this explains the async behaviour of JS while using the &lt;strong&gt;fs&lt;/strong&gt; module here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The code uses a &lt;strong&gt;callback&lt;/strong&gt; (oldest and non preferred method to implement asynchronous behaviour in JavaScript) function having &lt;code&gt;err, result&lt;/code&gt; as the parameters.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Detailed explanation:&lt;/strong&gt; When Node is parsing the code, as soon as it comes to the function call of &lt;code&gt;readFile()&lt;/code&gt; which is an async function, it encounters a callback (or &lt;a href="https://www.geeksforgeeks.org/what-is-callback-hell-in-node-js/"&gt;callback hell&lt;/a&gt; in this case) which rather than halting the code, lets it execute continuously, while waiting for the response from the &lt;code&gt;readFile()&lt;/code&gt; function. Thus, the synchronous console.logs, which are outside of &lt;code&gt;readFile()&lt;/code&gt; are executed first, letting the asynchronous code finish in parallel.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
