Open In App

Asynchronous Programming in NodeJS

Last Updated : 21 Feb, 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.

Understanding Asynchronous Programming

Asynchronous programming lets tasks run in the background without stopping the main execution. In NodeJS, this is important as it uses a non-blocking, event-driven architecture, making it efficient for handling multiple operations at once.

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 Asynchronous Programming Works in NodeJS?

NodeJS 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' ]

Challenges with Callbacks

  • Callback Hell: When multiple callbacks are nested inside each other, the code becomes deeply indented, making it hard to read and maintain.
  • Error Handling: Dealing with errors in nested callbacks can be tricky, especially when handling multiple async tasks.
  • Code Readability: Too many nested callbacks create a messy structure, often called the “Pyramid of Doom,” which makes the code difficult to follow and debug.

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

 Advantages of Promises

  • Promises allow chaining, making the code cleaner and easier to manage.
  • They help in handling errors effectively with .catch().

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

Advantages of Async/Await

  • Makes asynchronous code look and behave like synchronous code.
  • It helps avoid deep nesting, making the code cleaner and easier to read.

Best Practices for Asynchronous Programming in NodeJS

Here are the some best practices of Asynchronous Programming in NodeJS

  • Use Promises or Async/Await: These are better than callbacks as they make the code more readable and easier to manage.
  • Handle Errors Properly: Always catch and manage errors in async code to prevent unexpected crashes.
  • Avoid Callback Hell: Structure your code well to prevent too many nested callbacks, which can make maintenance difficult.
  • Use the Event Loop Wisely: Make sure long-running tasks don’t block the event loop so the app stays responsive.

Conclusion

Asynchronous programming is at the core of NodeJS, allowing multiple tasks to run efficiently without blocking execution. Features like the event loop, callbacks, promises, and async/await help keep things fast and smooth, especially for I/O operations. By using best practices—such as handling errors properly, avoiding callback hell, and leveraging promises or async/await—developers can build scalable and high-performance applications.



Similar Reads