Skip to content

Instantly share code, notes, and snippets.

@anatooly
Created November 2, 2020 16:13
Show Gist options
  • Save anatooly/74aaeabdd609cbf6b8ed37664e73bdf0 to your computer and use it in GitHub Desktop.
Save anatooly/74aaeabdd609cbf6b8ed37664e73bdf0 to your computer and use it in GitHub Desktop.
Fetch abort
/**
* @docs https://dev.to/ruheni/fetch-is-all-you-need-e8f
*/
const controller = new AbortController();
const signal = controller.signal;
let url = 'https://jsonplaceholder.typicode.com/todos/1'
setTimeout(() => controller.abort(), 100);
const fetchSomeData = () => {
fetch(url, { signal })
.then(res => res.json())
.then(data => console.log(data))
.catch(error => {
if (error.name = 'AbortError') {
console.log('You just aborted a fetch!💔')
}
})
}
fetchSomeData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment