Open In App

Asynchronous Programming in NodeJS

Last Updated : 19 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Asynchronous programming in NodeJS allows tasks to run in the background without blocking execution, enabling efficient handling of multiple operations. It uses the event loop, callbacks, promises, and async/await to manage non-blocking I/O tasks seamlessly.

Why Use Asynchronous Programming in NodeJS?

Non-Blocking I/O: NodeJS is commonly used for tasks like file operations, API calls, and database queries. With async programming, these tasks run in the background without stopping the rest of the code.

  • Improved Performance & Scalability: This improves performance and scalability by allowing multiple requests to be handled at the same time without delays.
  • Event-Driven Execution: NodeJS follows an event-driven approach where the event loop listens for tasks and runs them only when needed.
  • Faster API Calls: This makes API calls faster since multiple data sources can be queried at the same time, reducing wait time.
  • Optimized CPU & Memory Usage: Unlike multi-threaded models, NodeJS uses a single-threaded event loop, which optimizes CPU and memory usage by avoiding unnecessary resource consumption.

How Does Asynchronous Programming Work in NodeJS?

It offers different ways to handle async operations, making sure tasks run smoothly without blocking execution and ensuring better performance.

Callbacks

A callback is a function passed into another function as an argument, which is then invoked inside the outer function to complete some routine or action. This function is called when the asynchronous operation is completed.

javascript
arr = ["Geeks", "Geeks", "pop", "Geeks"]
console.log("calling")

let value = arr.filter(function (element) {
    if (element != "pop")
        return element
});

console.log("called")
console.log(value)
  • The filter method checks each element of the array. If the element is not "pop", it’s included in the new array.
  • The console.log statements show the non-blocking behavior of the operation.

Output

callingcalled[ 'Geeks', 'Geeks', 'Geeks' ]

Promises

A Promise is an object that represents the outcome of an asynchronous task, whether it's completed now or will be resolved later.

javascript
const multiplication = (numberOne, numberTwo) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (numberOne < 0 || numberTwo < 0) {
                return reject("Only positive numbers allowed");
            }
            resolve(numberOne * numberTwo);
        }, 1000);
    });
};

// Call for positive numbers
multiplication(5, 3)
    .then((product) => {
        console.log("The product is:", product);
    })
    .catch((error) => {
        console.log(error);
    });

// Call for negative numbers
multiplication(5, -3)
    .then((product) => {
        console.log("The product is:", product);
    })
    .catch((error) => {
        console.log(error);
    });
  • The multiplication function returns a promise. It multiplies two numbers after a 1-second delay.
  • If any number is negative, it rejects the promise with an error. Otherwise, it resolves with the result.

Output

The product is: 15Only Positive number allowed

3. Async/Await

Async/Await is a modern approach to handling async code, making it look and work like synchronous code. This improves readability and makes it easier to write and maintain.

javascript
function resolveLater() {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve('GeeksforGeeks');
        }, 2000);
    });
}

async function waitForGeeksforGeeks() {
    console.log('calling');
    const result = await resolveLater();
    console.log(result);
}

waitForGeeksforGeeks();
  • The resolveLater function returns a promise that resolves after 2 seconds.
  • The waitForGeeksforGeeks function is async/await keyword is used to pause the execution until the promise resolves.

Output

callingGeeksforGeeks

Explore