From 5efca1ebbc9df85ff88758a9523480eccca531d7 Mon Sep 17 00:00:00 2001 From: Nick Uraltsev Date: Sun, 18 Sep 2016 14:16:27 -0700 Subject: [PATCH] Updating docs --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c42a02..bb6abdc 100644 --- a/README.md +++ b/README.md @@ -305,7 +305,12 @@ These are the available config options for making requests. Only the `url` is re proxy: { host: '127.0.0.1', port: 9000 - } + }, + + // `cancelToken` specifies a cancel token that can be used to cancel the request + // (see Cancellation section below for details) + cancelToken: new CancelToken(function (cancel) { + }) } ``` @@ -457,6 +462,51 @@ axios.get('/user/12345', { }) ``` +## Cancellation + +You can cancel a request using a *cancel token*. + +> The axios cancel token API is based on [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises), which is currently at Stage 1. + +You can create a cancel token by passing an executor function to the `CancelToken` constructor as shown below: + +```js +var Cancel = axios.Cancel; +var CancelToken = axios.CancelToken; + +var cancel; + +axios.get('/user/12345', { + cancelToken: new CancelToken(function executor(c) { + // An executor function receives a cancel function as a parameter + // You can use the cancel function to cancel the request later + cancel = c; + }) +}).catch(function(thrown) { + if (thrown instanceof Cancel) { + console.log('Request canceled', thrown.message); + } else { + // handle error + } +}); + +// cancel the request (the message parameter is optional) +cancel('Operation canceled by the user.'); +``` + +You can also create a cancel token using the `CancelToken.source` factory: + +```js +var CancelToken = axios.CancelToken; +var source = CancelToken.source(); + +axios.get('/user/12345', { + cancelToken: source.token +}); + +source.cancel(); +``` + ## Semver Until axios reaches a `1.0` release, breaking changes will be released with a new minor version. For example `0.5.1`, and `0.5.4` will have the same API, but `0.6.0` will have breaking changes.