mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Adding TypeScript definitions for cancel tokens
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance, AxiosAdapter } from '../../';
|
||||
import axios, {
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
AxiosAdapter,
|
||||
Cancel,
|
||||
CancelToken,
|
||||
CancelTokenSource,
|
||||
Canceler
|
||||
} from '../../';
|
||||
|
||||
import { Promise } from 'es6-promise';
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
@@ -30,7 +41,8 @@ const config: AxiosRequestConfig = {
|
||||
proxy: {
|
||||
host: '127.0.0.1',
|
||||
port: 9000
|
||||
}
|
||||
},
|
||||
cancelToken: new axios.CancelToken((cancel: Canceler) => {})
|
||||
};
|
||||
|
||||
const handleResponse = (response: AxiosResponse) => {
|
||||
@@ -210,3 +222,18 @@ axios.get('/user')
|
||||
axios.get('/user')
|
||||
.catch((error: any) => Promise.resolve('foo'))
|
||||
.then((value: string) => {});
|
||||
|
||||
// Cancellation
|
||||
|
||||
const source: CancelTokenSource = axios.CancelToken.source();
|
||||
|
||||
axios.get('/user', {
|
||||
cancelToken: source.token
|
||||
}).catch((thrown: AxiosError | Cancel) => {
|
||||
if (thrown instanceof axios.Cancel) {
|
||||
const cancel: Cancel = thrown;
|
||||
console.log(cancel.message);
|
||||
}
|
||||
});
|
||||
|
||||
source.cancel('Operation has been canceled.');
|
||||
|
||||
Reference in New Issue
Block a user