Reference / JavaScript
AbortController
A controller object that allows you to abort one or more Web requests or asynchronous tasks on demand via an AbortSignal.
Syntax
new AbortController(): AbortControllerExamples
Aborting a fetch request with timeout
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);
try {
const res = await fetch('/api/data', { signal: controller.signal });
} catch (err) {
if (err.name === 'AbortError') console.warn('Request timed out');
} finally {
clearTimeout(timeout);
}Supported natively in Node.js 15+ and all modern browsers.