Promise.all
Start several tasks together and wait until every one finishes.
Promise.all([p1, p2, p3]) runs work in parallel
and fulfills with an array of results in the same order.
Wall-clock time is about the longest delay — not the sum.
If any promise rejects, the whole Promise.all
rejects (one fail fails all).
Results
—
Timing
—
Important JavaScript
const a = delayValue("Ava", 400);
const b = delayValue("Ben", 700);
const c = delayValue("Cara", 500);
Promise.all([a, b, c])
.then(function (names) {
// ["Ava", "Ben", "Cara"] — wall time ≈ 700ms, not 1600ms
console.log(names);
})
.catch(function (error) {
// If any one rejects, you land here
console.log(error.message);
});