mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Adding cancellation support
This commit is contained in:
@@ -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