AbortController: abort() method
The abort() method aborts the signal, and every request that was given this signal is canceled.
Syntax
abort()
abort(reason)Parameters
reason
The reason for cancellation, any value; optional. It becomes signal.reason. When omitted, signal.reason is an Error whose name is 'AbortError' and whose message is The operation was aborted.
Return value
None (undefined).
Exceptions
None.
Description
- When called,
signalfires theabortevent once. - Calling it again has no effect: the
abortevent does not fire again, andreasondoes not change. - Cancellation cannot be undone.
- When a request is canceled while in progress,
fetch()andread()throw notreasonbut a newAbortError.
Examples
js
const controller = new AbortController();
controller.abort('The user left the page');
controller.abort('Second time');
console.log(controller.signal.aborted, controller.signal.reason);Result: Prints true The user left the page. The second call has no effect.
Differences from the web
- When
reasonis omitted,reasonis anErrorwhosenameis'AbortError'. On the web it is aDOMException. - When a request is canceled while in progress,
fetch()throws a newAbortError, notreason.