NODEJS Contents

AbortController in Node (Canceling Work)

Learn how to cancel asynchronous operations in Node.js using AbortController and why cancellation is critical for robust systems.

On this page

Why Cancellation Matters

In production systems, requests may need to be canceled: client disconnects, timeouts, or shutdown events. Without cancellation support, resources remain busy unnecessarily.

AbortController Basics

const controller = new AbortController();
const signal = controller.signal;

setTimeout(() => controller.abort(), 1000);

Using with fetch

await fetch('https://example.com', { signal });

Handling Abort

try {
  await fetch(url, { signal });
} catch (err) {
  if (err.name === 'AbortError') {
    console.log('Request cancelled');
  }
}

Production Insight

  • Always cancel long-running external calls.
  • Propagate cancellation downstream.
  • Combine with graceful shutdown logic.