Course contentsShow
← JavaScript
Lesson 2307 of 2,72347. Networking PrimitivesFree lesson

Combining Signals with AbortSignal.any

Merge multiple AbortSignals into one that aborts when any parent signal aborts.

Combining Signals with AbortSignal.any

What you'll learn: How to merge multiple AbortSignals into a single signal that aborts when any of its parent signals aborts.

The Core Idea

Sometimes you need a fetch request (or other abortable operation) to respond to multiple different cancellation triggers. For example, you might want to cancel when either the user clicks a "Cancel" button or a timeout expires or the component unmounts. Instead of manually tracking multiple signals, AbortSignal.any() creates a composite signal that automatically aborts when any of its parent signals aborts.

Think of it like an "OR" gate: if any input goes off, the whole thing triggers.

How It Works

const userController = new AbortController();
const timeoutSignal = AbortSignal.timeout(5000);

// Combined signal aborts if EITHER parent aborts
const combinedSignal = AbortSignal.any([
  userController.signal,
  timeoutSignal
]);

fetch('/api/data', { signal: combinedSignal })
  .then(response => response.json())
  .catch(err => {
    if (err.name === 'AbortError') {
      console.log('Aborted by user OR timeout');
    }
  });

// User cancels after 2 seconds
setTimeout(() => userController.abort(), 2000);
// Fetch aborts immediately, timeout never fires

The combined signal listens to all parent signals. When the first parent aborts, the combined signal aborts too. You can pass as many signals as needed into the array.

Practical Use Cases

This is perfect for complex UI scenarios: cancel on user action, timeout, navigation, or component cleanup—all without juggling multiple signal checks.

Key Takeaway: AbortSignal.any() merges multiple signals into one that aborts when any parent signal aborts, simplifying multi-condition cancellation logic.