Skip to content
jshacks.dev
Async

Run promises in parallel with Promise.all

Run promises in parallel with Promise.all in JavaScript, waiting for every result in order and failing fast on the first rejection. Node.js 24.

1 min read
Summarize in AI

Awaiting in a loop runs one request at a time. Promise.all starts them together and returns the results in the order you gave.

JavaScript
const delay = (ms, v) => new Promise((r) => setTimeout(r, ms, v));

const started = Date.now();
const results = await Promise.all([delay(30, 'a'), delay(10, 'b'), delay(20, 'c')]);
const took = Date.now() - started;

console.log(results);
console.log(took < 55);
Text
[ 'a', 'b', 'c' ]
true

All three timers start at once, so the total is about the longest delay, 30 ms, not the sum. The result array follows the order of the input, not the order of completion, so 'a' is first although it finished last. See Promise.all.

If any promise rejects, Promise.all rejects at once with that error, and the other results are lost, although the other operations keep running. Use Promise.allSettled when you need every outcome. With thousands of items, limit concurrency instead of starting all of them.

Taggednode 24
Share

More snippets

Async

Sleep for a number of milliseconds

Sleep for a number of milliseconds in JavaScript with a promise around setTimeout, or timers/promises in Node.js 24, and await it.

1 min read
Node.js

Read a text file with fs promises

Read a text file with fs promises in Node.js: await readFile with an encoding, and catch the error code when the file is missing. Node.js 24.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.