Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Created October 1, 2021 09:19
Show Gist options
  • Save Kolenov/095c68f0d1033943960af2b7dedd268a to your computer and use it in GitHub Desktop.
Save Kolenov/095c68f0d1033943960af2b7dedd268a to your computer and use it in GitHub Desktop.
AbortController
useEffect(() => {
const abortController = new AbortController() // creating an AbortController
fetch(url, { signal: abortController.signal }) // passing the signal to the query
.then(data => {
setState(data) // if everything went well, set the state
})
.catch(error => {
if (error.name === 'AbortError') return // if the query has been aborted, do nothing
throw error
})
return () => {
abortController.abort() // stop the query by aborting on the AbortController on unmount
}
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment