A Shared Signal Cancels Promise.all Siblings

wrapping Promise.all jobs so the first rejection aborts a shared signal stops sibling tasks mid-flight instead of letting them finish into an unread result.

JavascriptResilience

Code

javascriptsnippet
46 lines
// A shared AbortSignal lets one failure cancel sibling work that is still
// running — otherwise Promise.all rejects while the survivors keep going
const events = [];
function task(name, ms, signal, fail) {
  return new Promise((resolve, reject) => {
    let settled = false;
    const timer = setTimeout(() => {
      settled = true;
      if (fail) reject(new Error(name + ' failed'));
      else resolve(name + ' done');
    }, ms);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      // reject() on an already-settled promise is a no-op; only log tasks
      // the abort actually stopped
      if (!settled) events.push(name + ' aborted');
      reject(new Error(name + ' aborted'));
    }, { once: true });
  });
}

const controller = new AbortController();
const { signal } = controller;
const jobs = [
  task('uploader', 20, signal, false),
  task('validator', 40, signal, true),
  task('reporter', 1000, signal, false),
];

// Wrap each job: the first rejection aborts the shared signal, and abort()
// is idempotent so later failures cannot re-trigger anything
const started = Date.now();
const guarded = jobs.map((job) =>
  job.catch((err) => {
    controller.abort();
    throw err;
  }),
);

try {
  await Promise.all(guarded);
} catch (err) {
  console.log('Promise.all rejected with:', err.message);
}
console.log('siblings stopped:', events.join(', '));
console.log('stopped before slow deadline:', Date.now() - started < 500);

Output

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

Promise.all rejected with: validator failed
siblings stopped: reporter aborted
stopped before slow deadline: true

How the snippet works

A Shared Signal Cancels Promise.all Siblings keeps everything in the function task.

The async parts await their results, which keeps ordering predictable.

It builds and returns a fresh value rather than reusing its inputs.

Caveats

  • No package installation, no environment variables, no setup steps.

Using it

Entry signature: function task(name, ms, signal, fail)

The run emits 3 labeled sections — Promise.all rejected with:, siblings stopped:, stopped before slow deadline: — one per step.

Key statements

signal.addEventListener('abort', () => {
  clearTimeout(timer);
  // reject() on an already-settled promise is a no-op; only log tasks
  // the abort actually stopped
  if (!settled) events.push(name + ' aborted');
  reject(new Error(name + ' aborted'));
}, { once: true });
await Promise.all(guarded);
job.catch((err) => {
  controller.abort();
  throw err;
}),

Interactive Tools for This Topic

FAQ

Does `task` modify its input?
The input survives unchanged; what you get back is a newly constructed value.
Does the code pull in any external packages?
No external packages are involved; every call resolves to a built-in API. The file defines `task` at load time.