Created
December 22, 2019 14:39
-
-
Save kephin/a63587c848d39c749639b8cc3f8fec89 to your computer and use it in GitHub Desktop.
cancel axios request
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
/* | |
<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