Quick Tip — Working with AbortController

Stop Fetching Please

The AbortController is an object that can help you aborting fetch requests. How do you do that? First of all, create a new AbortController:

const abortCtrl = new AbortController();
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
fetch(url, { signal }).then((response) => {
...
}).catch((e) => {
...
});
abortCtrl.abort();
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
fetch(url, { signal }).then((response) => {
...
}).catch((e) => {
if (error.code === 20) { // abort occurred
// Do something after abort occurred
}
});

Summary

This short post explained what is the AbortContoller and how to use it in your code. Let me know in the comments if you found it useful.

--

--

Hardcore web developer, @sparXys CEO, Google Web Technologies GDE, Microsoft MVP, Pro SPA Development co-author, husband, dad and a geek.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store