Created
November 2, 2020 16:13
-
-
Save anatooly/74aaeabdd609cbf6b8ed37664e73bdf0 to your computer and use it in GitHub Desktop.
Fetch abort
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
/** | |
* @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