Skip to content

Instantly share code, notes, and snippets.

@ivanalejandro0
Created July 26, 2018 19:00
Show Gist options
  • Save ivanalejandro0/ec3fd1cb9c446d40364689f0e7ab0e4f to your computer and use it in GitHub Desktop.
Save ivanalejandro0/ec3fd1cb9c446d40364689f0e7ab0e4f to your computer and use it in GitHub Desktop.
A simple snippet on how to use the fetch cancellation api.
// For more information see:
// https://developers.google.com/web/updates/2017/09/abortable-fetch
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
// https://caniuse.com/#search=fetch
const url = 'https://parrot.live';
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => controller.abort(), 1000);
fetch(url, { signal })
.then(response => response.text())
.then(text => console.log(text))
.catch(err => {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
console.error('Uh oh, an error!', err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment