Last active
February 14, 2021 17:14
-
-
Save taai/d5c3b35ad8a680f10e7c927bad8cd6d7 to your computer and use it in GitHub Desktop.
JavaScript AbortController example
This file contains hidden or 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
const url = 'big.mkv'; | |
const abortController = new AbortController(); | |
setTimeout(function() { | |
abortController.abort(); | |
}, 1); | |
async function fetchVideo() { | |
try { | |
const response = await fetch(url, { signal: abortController.signal }); | |
// this must be awaited too | |
const data = await response.blob(); // or .json() or .text() or .formData() | |
console.info('Download successful.'); | |
} catch (e) { | |
if (e instanceof DOMException && e.name === 'AbortError') { | |
console.error('The fetch request was aborted.'); | |
} | |
} | |
} | |
fetchVideo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment