2
0
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:
Nick Uraltsev
2016-09-17 11:52:56 -07:00
parent df50698d5a
commit 72dd897bb5
7 changed files with 139 additions and 10 deletions
+27 -9
View File
@@ -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);
});
};