Promise.all Fails Fast On First Rejection

Promise.all rejects as soon as any member rejects, abandoning slower pending results, while allSettled waits for every member and aligns statuses by position.

JavascriptResilience

Code

javascriptsnippet
18 lines
const ok = (name, ms) => new Promise((resolve) => setTimeout(() => resolve(name), ms));
const boom = (name, ms) => new Promise((_, reject) => setTimeout(() => reject(new Error(name)), ms));

// Promise.all 任一 reject 立即整体 reject:慢的成功者不会等
try {
  await Promise.all([ok("fast", 20), boom("bad", 5), ok("slow", 200)]);
} catch (reason) {
  console.log("failed with:", reason.message);
}

// 对照 allSettled:等全部结束,逐位对齐结果状态
const settled = await Promise.allSettled([ok("fast", 20), boom("bad", 5)]);
console.log(settled.map((s) => s.status).join(","));
console.log(settled.filter((s) => s.status === "fulfilled").map((s) => s.value).join(","));

// 全部成功时 all 按提交顺序聚合每个值
const values = await Promise.all([ok("a", 5), ok("b", 10)]);
console.log("all ok:", values.join(","));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

failed with: bad
fulfilled,rejected
fast
all ok: a,b

How it works

Promise.all Fails Fast On First Rejection keeps everything in the functions ok and boom.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Putting it to use

The primary signature is const ok = (name, ms)

The demo output is labeled (failed with:, all ok:); each label names the call that printed it.

Core lines

const boom = (name, ms) => new Promise((_, reject) => setTimeout(() => reject(new Error(name)), ms));
const ok = (name, ms) => new Promise((resolve) => setTimeout(() => resolve(name), ms));
const settled = await Promise.allSettled([ok("fast", 20), boom("bad", 5)]);

Interactive Tools for This Topic

FAQ

Will the snippet run without installing packages?
Yes, it runs as-is — nothing outside the built-in APIs is referenced. The file defines `ok`, `boom` at load time.