Skip to content

Instantly share code, notes, and snippets.

@kephin
Created December 22, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kephin/a63587c848d39c749639b8cc3f8fec89 to your computer and use it in GitHub Desktop.
Save kephin/a63587c848d39c749639b8cc3f8fec89 to your computer and use it in GitHub Desktop.
cancel axios request
/*
<button onclick="start()">Start</button>
<button onclick="cancel()">Cancel</button>
*/
let cancelTokenSource
const start = () => {
cancelTokenSource = axios.CancelToken.source()
axios
.get('http://localhost:8080', { cancelToken: cancelTokenSource.token })
.catch(thrown => {
if (axios.isCancel(thrown)) console.log('Request canceled', thrown.message)
})
}
const cancel = () => cancelTokenSource.cancel('Operation canceled by the user.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment