0
0
NodejsHow-ToBeginner · 4 min read

How to Run Parallel Async Operations in Node.js Efficiently

In Node.js, you can run parallel async operations by using Promise.all with async/await. This runs multiple promises at the same time and waits for all to finish, improving performance over sequential execution.
📐

Syntax

Use Promise.all to run multiple async operations in parallel. Pass an array of promises to Promise.all, and it returns a single promise that resolves when all have completed.

Example parts:

  • async function: Declares an asynchronous function.
  • await Promise.all([...]): Waits for all promises in the array to resolve.
  • Each item in the array is a promise representing an async operation.
javascript
async function runParallel() {
  const results = await Promise.all([
    asyncOperation1(),
    asyncOperation2(),
    asyncOperation3()
  ]);
  return results;
}
💻

Example

This example shows three async operations running in parallel using Promise.all. Each operation waits a different time, but the total time is close to the longest single wait, not the sum.

javascript
function wait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function asyncOperation1() {
  await wait(1000);
  return 'Result 1';
}

async function asyncOperation2() {
  await wait(2000);
  return 'Result 2';
}

async function asyncOperation3() {
  await wait(1500);
  return 'Result 3';
}

async function runParallel() {
  console.time('Parallel');
  const results = await Promise.all([
    asyncOperation1(),
    asyncOperation2(),
    asyncOperation3()
  ]);
  console.timeEnd('Parallel');
  console.log(results);
}

runParallel();
Output
Parallel: 2005.123ms [ 'Result 1', 'Result 2', 'Result 3' ]
⚠️

Common Pitfalls

Common mistakes when running parallel async operations include:

  • Using await inside a loop sequentially, which runs operations one after another instead of in parallel.
  • Not handling errors properly, since Promise.all rejects immediately if any promise fails.
  • Forgetting to return promises from async functions.

Correct approach is to start all promises first, then await them together.

javascript
async function wrongSequential() {
  const results = [];
  for (const op of [asyncOperation1, asyncOperation2, asyncOperation3]) {
    const result = await op(); // waits each one fully before next
    results.push(result);
  }
  return results;
}

async function rightParallel() {
  const promises = [asyncOperation1(), asyncOperation2(), asyncOperation3()];
  return await Promise.all(promises); // runs all at once
}
📊

Quick Reference

Tips for running parallel async operations in Node.js:

  • Use Promise.all to run multiple promises in parallel.
  • Wrap your code in async functions to use await cleanly.
  • Handle errors with try/catch or .catch() on promises.
  • Remember Promise.allSettled if you want to wait for all promises regardless of errors.

Key Takeaways

Use Promise.all with async/await to run async operations in parallel efficiently.
Avoid awaiting inside loops to prevent sequential execution.
Handle errors carefully because Promise.all fails fast on any rejected promise.
Promise.allSettled is useful when you want all results regardless of errors.