patternModerate
AbortController for cancellable async operations
Viewed 0 times
AbortSignal.any() requires Node 20+
AbortControllerAbortSignalcancel fetchsignaltimeoutabort
browsernodejs
Problem
Need to cancel in-flight fetch requests, timeouts, or async operations when component unmounts or a newer request supersedes an older one.
Solution
AbortController provides standard cancellation: const controller = new AbortController(); fetch(url, { signal: controller.signal }); controller.abort(). Works with: fetch, addEventListener (signal option), Node.js streams. For timeouts: AbortSignal.timeout(5000). Combine: AbortSignal.any([signal1, signal2]). In React: create in useEffect, abort in cleanup.
Why
Without cancellation, abandoned requests waste resources and cause bugs when stale responses arrive after the consumer is gone. AbortController is the web standard for cooperative cancellation.
Revisions (0)
No revisions yet.