Asynchronous Functions and the Node.js Event Loop Last Updated : 14 Oct, 2021 Comments Improve Suggest changes 5 Likes Like Report Asynchronous Functions Everyone knows JavaScript is asynchronous in nature and so is the Node. The fundamental principle behind Node is that an application is executed on a single thread or process and the events are thus handled asynchronously. If we consider any typical web server like Apache, it requires separate threads for each process until the request is satisfied. The disadvantage in using multi-thread is they are not memory intensive and doesn’t scale very well. Also, we have to ensure that each process must be thread safe and deadlock should not appear. But Node does things differently. On starting a Node application, it creates only a single thread of execution. When the Node receives a request, it assigns the thread to that process and no other request can be processed until it has finished processing the code for the current request. Therefore, Node handles multiple requests at the same time by using event loop and callback functions. An Event Loop is a type of functionality which basically polls for specific events and invokes event handlers when required. A Callback Function is this event handler in Node. In Node applications, the Node initiates the request but does not wait around the request to get the response. Instead of that, it attaches a callback function to the request. When the request has been completed or the response has been received by the request, the callback function emits an event to do something with either the results of the requested action or the resource requested. If multiple people access a Node application at the same time, and the application needs to access a resource from a file, Node attaches a callback function with each request. As soon as the resource becomes available to that particular request, a callback function is called to each person’s request. The Node can handle other requests in the meantime. The serving of the parallel requests in the Node application depends upon how busy the application is and how it is designed? Examples: javascript // Normal Function function add(a,b){ return a+b; } // Async Function async function asyncadd(a,b){ Return a+b; } Asynchronously opening and writing the contents of a file javascript // load http module var http = require('http'); var fs = require('fs'); // load http module var http = require('http'); var fs = require('fs'); // create http server http.createServer(function (req, res) { // open and read in helloworld.js fs.readFile('helloworld.js', 'utf8', function(err, data) { res.writeHead(200, {'Content-Type': 'text/plain'}); if (err) res.write('Could not find or open file for reading\n'); else // if no error, writing JS file to a client res.write(data); res.end(); }); }).listen(8124, function() { console.log('bound to port 8124');}); console.log('Server running on 8124/'); Create Quiz Comment M mayank021 Follow 5 Improve M mayank021 Follow 5 Improve Article Tags : Node.js Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like