mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
refactor(http): use ClientRequest "aborted" value (#966)
This commit is contained in:
committed by
Rubén Norte
parent
46e275c407
commit
f1fb3de38f
@@ -19,7 +19,6 @@ module.exports = function httpAdapter(config) {
|
|||||||
var data = config.data;
|
var data = config.data;
|
||||||
var headers = config.headers;
|
var headers = config.headers;
|
||||||
var timer;
|
var timer;
|
||||||
var aborted = false;
|
|
||||||
|
|
||||||
// Set User-Agent (required by some servers)
|
// Set User-Agent (required by some servers)
|
||||||
// Only set header if it hasn't been set in config
|
// Only set header if it hasn't been set in config
|
||||||
@@ -129,7 +128,7 @@ module.exports = function httpAdapter(config) {
|
|||||||
|
|
||||||
// Create the request
|
// Create the request
|
||||||
var req = transport.request(options, function handleResponse(res) {
|
var req = transport.request(options, function handleResponse(res) {
|
||||||
if (aborted) return;
|
if (req.aborted) return;
|
||||||
|
|
||||||
// Response has been received so kill timer that handles request timeout
|
// Response has been received so kill timer that handles request timeout
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
@@ -177,7 +176,7 @@ module.exports = function httpAdapter(config) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
stream.on('error', function handleStreamError(err) {
|
stream.on('error', function handleStreamError(err) {
|
||||||
if (aborted) return;
|
if (req.aborted) return;
|
||||||
reject(enhanceError(err, config, null, lastRequest));
|
reject(enhanceError(err, config, null, lastRequest));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -195,7 +194,7 @@ module.exports = function httpAdapter(config) {
|
|||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
req.on('error', function handleRequestError(err) {
|
req.on('error', function handleRequestError(err) {
|
||||||
if (aborted) return;
|
if (req.aborted) return;
|
||||||
reject(enhanceError(err, config, null, req));
|
reject(enhanceError(err, config, null, req));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -204,20 +203,16 @@ module.exports = function httpAdapter(config) {
|
|||||||
timer = setTimeout(function handleRequestTimeout() {
|
timer = setTimeout(function handleRequestTimeout() {
|
||||||
req.abort();
|
req.abort();
|
||||||
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
|
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
|
||||||
aborted = true;
|
|
||||||
}, config.timeout);
|
}, config.timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.cancelToken) {
|
if (config.cancelToken) {
|
||||||
// Handle cancellation
|
// Handle cancellation
|
||||||
config.cancelToken.promise.then(function onCanceled(cancel) {
|
config.cancelToken.promise.then(function onCanceled(cancel) {
|
||||||
if (aborted) {
|
if (req.aborted) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.abort();
|
req.abort();
|
||||||
reject(cancel);
|
reject(cancel);
|
||||||
aborted = true;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user