Created
July 26, 2018 19:00
-
-
Save ivanalejandro0/ec3fd1cb9c446d40364689f0e7ab0e4f to your computer and use it in GitHub Desktop.
A simple snippet on how to use the fetch cancellation api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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