How to Write Asynchronous Function for Node.js ? Last Updated : 24 Sep, 2024 Comments Improve Suggest changes Like Article Like Report The asynchronous function can be written in Node.js using 'async' preceding the function name. The asynchronous function returns an implicit Promise as a result. The async function helps to write promise-based code asynchronously via the event loop. Async functions will always return a value.Await function can be used inside the asynchronous function to wait for the promise. This forces the code to wait until the promise returns a result.Steps to Write Asynchronous Function for Node.jsStep 1: Create a project folderUse the following command to initialize the package.json file inside the project folder.npm init -yStep 2: Install async using the following command:npm i asyncNote: Installation of async library only required in older versions of node.Step 3: Create a server.js file & write the following code inside it.Run the code using the below commandnpm startExample 1: Create an asynchronous function to calculate the square of a number inside Node.js. JavaScript const async = require("async"); function square(x) { return new Promise((resolve) => { setTimeout(() => { resolve(Math.pow(x, 2)); }, 2000); }); } async function output(x) { const ans = square(x); console.log(ans); } output(10); const async = require("async"); function square(x) { return new Promise((resolve) => { setTimeout(() => { resolve(Math.pow(x, 2)); }, 2000); }); } async function output(x) { const ans = await square(x); console.log(ans); } output(10); Output:Example 2: Create an asynchronous function to calculate the sum of two numbers inside Node.js using await. Perform the above procedure to create a Node.js project. JavaScript const async = require("async"); function square(a, b) { return new Promise(resolve => { setTimeout(() => { resolve(a + b); }, 2000); }); } async function output(a, b) { const ans = await square(a, b); console.log(ans); } output(10, 20); Output: Comment More infoAdvertise with us Next Article How to Write Asynchronous Function for Node.js ? C chaitanyashah707 Follow Improve Article Tags : JavaScript Web Technologies Node.js NodeJS-Questions Similar Reads How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read How to Return an Array from Async Function in Node.js ? Asynchronous programming in Node.js is fundamental for handling operations like network requests, file I/O, and database queries without blocking the main thread. One common requirement is to perform multiple asynchronous operations and return their results as an array. This article explores how to 4 min read How to chain asynchronous functions in JavaScript ? JavaScript is a single-threaded, asynchronous programming language. Thus, some time-consuming operations like I/O, accessing the database, network calls, etc. are performed asynchronously so that it does not interrupt things in the only JS thread. This can be done by asynchronous code like promises 2 min read How to convert an asynchronous function to return a promise in JavaScript ? In this article, we will learn how to convert an asynchronous function to return a promise in JavaScript. Approach: You need to first declare a simple function (either a normal function or an arrow function (which is preferred)). You need to create an asynchronous function and then further you need 3 min read How to handle asynchronous operations in Node ? NodeJS, renowned for its asynchronous and event-driven architecture, offers powerful mechanisms for handling asynchronous operations efficiently. Understanding how to manage asynchronous operations is crucial for NodeJS developers to build responsive and scalable applications. What are Asynchronous 2 min read Like