This code aborts the request after 10 ms:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://jsonplaceholder.typicode.com/todos/1', {
signal
})
.then(response => response.json())
.then(data => {
console.log('Data:', data);
})
.catch(err => {
console.log(err)
if (err.name === 'AbortError')....
});
setTimeout(() => {
controller.abort();
}, 10);
Output :
DOMException: signal is aborted without reason
Question:
If it says "without a reason," is it possible to supply a reason?
Because otherwise, the message is a bit strange, as if I could supply a reason but didn't.
You can supply a reason as an argument to controller.abort()
, like controller.abort("took longer than 10 milliseconds")
From MDN:
abort()
abort(reason)
reason (optional): The reason why the operation was aborted, which can be any JavaScript value. If not specified, the reason is set to "AbortError" DOMException.
https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort