<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vishal K on Medium]]></title>
        <description><![CDATA[Stories by Vishal K on Medium]]></description>
        <link>https://medium.com/@vishalkalaskar?source=rss-f980314fb9de------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yxMpetAZSKb-gP4F.</url>
            <title>Stories by Vishal K on Medium</title>
            <link>https://medium.com/@vishalkalaskar?source=rss-f980314fb9de------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:13:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vishalkalaskar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Event Loop in NodeJS: How It Works!]]></title>
            <link>https://medium.com/@vishalkalaskar/we-must-have-come-across-the-terms-callbacks-non-blocking-i-o-and-event-looping-while-927a5e2150c0?source=rss-f980314fb9de------2</link>
            <guid isPermaLink="false">https://medium.com/p/927a5e2150c0</guid>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactor-pattern]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Vishal K]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 17:45:29 GMT</pubDate>
            <atom:updated>2019-10-31T16:17:06.480Z</atom:updated>
            <content:encoded><![CDATA[<p>We must have come across the terms ‘Callbacks’, ‘Non-blocking I/O’ and ‘Event Looping’ while learning or understanding Node-JS.</p><p>Here is an attempt to explain!</p><p>What is Event Looping?</p><p>At start, we will go through concepts of Blocking and Non-Blocking executions, then jumping on to the ‘Event Loops by Reactor Pattern’!</p><p>Consider examples like reading a file into the application, or an http request to a third party api’s. There are two ways we can perform these I/O operations:</p><p>Blocking I/O: Where, application will make a function call and pause its execution at this point until the data is received.</p><p>Non-Blocking I/O: Where, application will make a function call, and, without waiting for the results, continue its execution.</p><p>Former is called as ‘Synchronous’, while later is called as Asynchronous’ nature of execution, and NodeJS as you know, is Asynchronous in nature.</p><p>So, despite being single-threaded framework, How NodeJS works asynchronously?Answer is, Reactor Pattern – which is at the heart of NodeJS.</p><p>But, what is Event Looping?</p><p>“It is a magical place filled with unicorns and rainbows.“</p><p>To explain this, we will compare two patterns of Event-Driven programming:</p><p>Busy Waiting:</p><p>In which, continuous checks on resources is done to see if data is ready for further processing, – called as Active Polling</p><p>resources = [socketA, socketB, pipeA];</p><p>while(!resources.isEmpty()) {<br> for(i=0; i &lt; resources.length; i++) {<br> resource = resources[i];<br> <br> // try to load<br> var data = resource.read();<br> if( data === NO_DATA_AVAILABLE)<br> // there is no data to read at the moment<br> continue;<br> <br> if( data === RESOURCE_CLOSED )<br> // the resource was closed, remove it from list<br> resources.remove(i);<br> else<br> // some data was received, process it<br> consumeData( data );<br> }<br>}</p><p>Though, it is the simple way to implement asynchronous, event-driven behaviour, it’s not efficient. As this process will keep running continuously, it will use the CPU extensively.</p><p>2. Reactor Pattern:</p><p>Reactor Pattern is made of following:</p><p>Resources:</p><p>That are shared by multiple applications for I/O operations, generally slower in executions.</p><p>Synchronous Event De-multiplexer / Event Notifier:</p><p>This uses Event Loop for blocking on all resources. When a set of I/O operations completes, the Event De-multiplexer pushes the new events into the Event Queue.</p><p>Event Loop and Event Queue:</p><p>Event Queue queues up the new events occurred along with its event-handler, &lt;event, event-handler&gt; pair.</p><p>Request Handler / Application:</p><p>This is, generally, the application that provides the handler to be executed for registered events on resources.</p><p>See below diagram for understanding of Reactor Pattern and the place for Event Loop:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*X0m82lpBhRONFvRGCRu84w.jpeg" /></figure><p>This is what happens in an application using the reactor pattern:</p><p>The application generates a new I/O operation by submitting a request to<br>the Event De-multiplexer. The application also specifies a handler, which will<br>be invoked when the operation completes. Submitting a new request to the<br>Event De-multiplexer is a non-blocking call and it immediately returns the<br>control back to the application.</p><p>When a set of I/O operations completes, the Event De-multiplexer pushes the<br>new events into the Event Queue.</p><p>At this point, the Event Loop iterates over the items of the Event Queue.</p><p>For each event, the associated handler is invoked.</p><p>The handler, which is part of the application code, will give back the<br>control to the Event Loop when its execution completes (5a). However,<br>new asynchronous operations might be requested during the execution<br>of the handler (5b), causing new operations to be inserted in the Event<br>De-multiplexer (1), before the control is given back to the Event Loop.</p><p>When all the items in the Event Queue are processed, the loop will block<br>again on the Event De-multiplexer which will then trigger another cycle.</p><p>The asynchronous behavior is now clear. The application expresses the interest to<br>access a resource at one point in time (without blocking) and provides a handler,<br>which will then be invoked at another point in time when the operation completes.</p><p>This is by far the basics behind asynchronous behavior of Node-JS.</p><p>Reach out to me on <a href="https://linkedin.com/in/vishalkalaskar">linkedin</a>. Do send me your thoughts in the comments below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=927a5e2150c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>