Glossary / Cancellation

Cancellation

Cancellation asks asynchronous work to stop because its result is no longer needed, a deadline expired, or an owner is shutting down. JavaScript Promises have no built-in cancellation state or universal cancel operation. Cancellation must be designed into the underlying API and propagated through the work graph; merely racing a Promise with a timeout stops waiting, not necessarily the operation.

Cooperative cancellation

Browser and modern Node APIs commonly use AbortSignal:

const controller = new AbortController();
const request = fetch(url, { signal: controller.signal });
controller.abort(); // request rejects with an abort-related error

Other operations may require a cancellation token, an explicit cancel() method, or a cooperative flag checked between chunks. CPU-bound synchronous JavaScript cannot process an abort event while it is monopolizing the thread; break work into chunks or use a worker that can receive cancellation messages. Cancellation is often best-effort: an operation may have already committed external side effects.

Define ownership, propagation, and cleanup: who can cancel, whether cancellation is idempotent, how it differs from failure, and whether partial results are retained. Ensure listeners and resources are released. Avoid treating every AbortError as a user-visible failure, while still distinguishing user cancellation from timeouts and genuine network errors.

Interview questions

  1. Are Promises cancellable? Not natively; underlying operations need an explicit cancellation mechanism.
  2. Does Promise.race cancel the loser? No.
  3. Can synchronous CPU work respond to AbortSignal immediately? No, not while blocking the same thread.
  4. What does cooperative cancellation require? A signal/token checked and honored by each operation boundary.

References