Promise.all
Run async operations in parallel
const delay = (ms, val) =>
new Promise(resolve => setTimeout(() => resolve(val), ms));
(async () => {
// Parallel — ~200ms total
console.time("parallel");
const results = await Promise.all([
delay(100, "first"),
delay(200, "second"),
delay(50, "third"),
]);
console.timeEnd("parallel");
console.log("results:", results);
// Same work serially — ~350ms total
console.time("serial");
const a = await delay(100, "first");
const b = await delay(200, "second");
const c = await delay(50, "third");
console.timeEnd("serial");
console.log([a, b, c]);
})();