mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
Adding cancellation support
This commit is contained in:
@@ -185,6 +185,15 @@ module.exports = function httpAdapter(config) {
|
||||
}, config.timeout);
|
||||
}
|
||||
|
||||
if (config.cancelToken) {
|
||||
// Handle cancellation
|
||||
config.cancelToken.promise.then(function onCanceled(cancel) {
|
||||
req.abort();
|
||||
reject(cancel);
|
||||
aborted = true;
|
||||
});
|
||||
}
|
||||
|
||||
// Send the request
|
||||
if (utils.isStream(data)) {
|
||||
data.pipe(req);
|
||||
|
||||
@@ -153,6 +153,15 @@ module.exports = function xhrAdapter(config) {
|
||||
request.upload.addEventListener('progress', config.onUploadProgress);
|
||||
}
|
||||
|
||||
if (config.cancelToken) {
|
||||
// Handle cancellation
|
||||
config.cancelToken.promise.then(function onCanceled(cancel) {
|
||||
request.abort();
|
||||
reject(cancel);
|
||||
// Clean up request
|
||||
request = null;
|
||||
});
|
||||
}
|
||||
|
||||
if (requestData === undefined) {
|
||||
requestData = null;
|
||||
|
||||
@@ -34,6 +34,10 @@ axios.create = function create(defaultConfig) {
|
||||
return createInstance(defaultConfig);
|
||||
};
|
||||
|
||||
// Expose Cancel & CancelToken
|
||||
axios.Cancel = require('./cancel/Cancel');
|
||||
axios.CancelToken = require('./cancel/CancelToken');
|
||||
|
||||
// Expose all/spread
|
||||
axios.all = function all(promises) {
|
||||
return Promise.all(promises);
|
||||
|
||||
@@ -2,6 +2,16 @@
|
||||
|
||||
var utils = require('./../utils');
|
||||
var transformData = require('./transformData');
|
||||
var Cancel = require('../cancel/Cancel');
|
||||
|
||||
/**
|
||||
* Throws a `Cancel` if cancellation has been requested.
|
||||
*/
|
||||
function throwIfCancellationRequested(config) {
|
||||
if (config.cancelToken) {
|
||||
config.cancelToken.throwIfRequested();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a request to the server using whichever adapter
|
||||
@@ -11,6 +21,8 @@ var transformData = require('./transformData');
|
||||
* @returns {Promise} The Promise to be fulfilled
|
||||
*/
|
||||
module.exports = function dispatchRequest(config) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
// Ensure headers exist
|
||||
config.headers = config.headers || {};
|
||||
|
||||
@@ -52,6 +64,8 @@ module.exports = function dispatchRequest(config) {
|
||||
// Wrap synchronous adapter errors and pass configuration
|
||||
.then(adapter)
|
||||
.then(function onFulfilled(response) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
// Transform response data
|
||||
response.data = transformData(
|
||||
response.data,
|
||||
@@ -60,16 +74,20 @@ module.exports = function dispatchRequest(config) {
|
||||
);
|
||||
|
||||
return response;
|
||||
}, function onRejected(error) {
|
||||
// Transform response data
|
||||
if (error && error.response) {
|
||||
error.response.data = transformData(
|
||||
error.response.data,
|
||||
error.response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
}, function onRejected(reason) {
|
||||
if (!(reason instanceof Cancel)) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
// Transform response data
|
||||
if (reason && reason.response) {
|
||||
reason.response.data = transformData(
|
||||
reason.response.data,
|
||||
reason.response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
return Promise.reject(reason);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user