2
0
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:
Nick Uraltsev
2016-09-17 12:49:14 -07:00
parent 72dd897bb5
commit 2033ef3ad0
2 changed files with 60 additions and 2 deletions
+29 -2
View File
@@ -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.');