Skip to content

Instantly share code, notes, and snippets.

@ooooak
Created June 28, 2021 12:18
Show Gist options
  • Save ooooak/22a6ba45e031b049fafff8d9828955f0 to your computer and use it in GitHub Desktop.
Save ooooak/22a6ba45e031b049fafff8d9828955f0 to your computer and use it in GitHub Desktop.
import { unAuthed } from '@utils/http';
import axios, { CancelTokenSource } from 'axios'
import { useState } from 'react';
export function AxiosAbortTest(){
let source: CancelTokenSource | undefined;
async function requestResource(){
if (source){
source.cancel('Operation canceled due to new request.');
}
source = axios.CancelToken.source();
unAuthed('/', {cancelToken: source.token}).catch(e => {
console.log(e);
})
}
return (
<a href="" onClick={async (e) =>{
e.preventDefault();
await requestResource()
}}>Send Request</a>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment