Created
February 9, 2020 08:33
-
-
Save alanhe421/52d2a8a04e4695d158ec61ba7977cf3f to your computer and use it in GitHub Desktop.
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
import axios from 'axios'; | |
/** | |
* Cancel previous pending request | |
*/ | |
const pending = new Map(); | |
export const addPending = config => { | |
const url = [config.method, config.url].join('&'); | |
config.cancelToken = | |
config.cancelToken || | |
new axios.CancelToken(cancel => { | |
if (!pending.has(url)) { | |
pending.set(url, cancel); | |
} | |
}); | |
}; | |
export const removePending = config => { | |
const url = [config.method, config.url].join('&'); | |
if (pending.has(url)) { | |
const cancel = pending.get(url); | |
cancel(url); | |
pending.delete(url); | |
} | |
}; | |
export const clearPending = () => { | |
for (const [url, cancel] of pending) { | |
cancel(url); | |
} | |
pending.clear(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment