Created
August 12, 2019 09:47
-
-
Save qmzik/3ed3ba3179502d86d35e8ff31d6658ba to your computer and use it in GitHub Desktop.
Cancelling request with axios in Vue.js
This file contains 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
import axios, { CancelTokenStatic } from 'axios'; | |
export const http = axios.create({ | |
baseURL: 'https://api.domen.com/', | |
}); | |
const cancelToken: CancelTokenStatic = axios.CancelToken; | |
export let cancelSource = cancelToken.source(); | |
export const refreshTokenForUpdateApp = (() => { | |
cancelSource = cancelToken.source(); | |
}); | |
export const cancelAllUserRequests = (() => { | |
cancelSource.cancel(); | |
}); |
This file contains 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
@Action | |
public async invokeRequests(): Promise<void> { | |
try { | |
updateAppCancel.cancel(); | |
refreshTokenForUpdateApp(); | |
const res = await http.get('/getsomething', | |
{ cancelToken: updateAppCancel.token }, | |
); | |
} catch (e) { | |
if (e.request === undefined) { | |
// handle cancelling error | |
} | |
} | |
} | |
@Action | |
public async dependedRequest(): Promise<void> { | |
try { | |
const res = await http.get('/getmore', | |
{ cancelToken: updateAppCancel.token }, | |
); | |
} catch (e) { | |
if (e.request === undefined) { | |
// handle cancel | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment