Skip to content

Instantly share code, notes, and snippets.

@taai
Last active February 14, 2021 17:14
Show Gist options
  • Save taai/d5c3b35ad8a680f10e7c927bad8cd6d7 to your computer and use it in GitHub Desktop.
Save taai/d5c3b35ad8a680f10e7c927bad8cd6d7 to your computer and use it in GitHub Desktop.
JavaScript AbortController example
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