Description
AbortController is a built in API that lets us cancel HTTP requests while they are in transit.
?
Reach for when
The user starts an action that we may want to cancel such as Debounce.
Code
const controller = new AbortController();
fetch(url, { signal: controller.signal })
.then(res => res.json())
.then(data => render(data))
.catch(err => {
if (err.name === 'AbortError') console.log('Request cancelled');
else console.error(err);
});
// Cancel:
controller.abort();