mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +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 headers = config.headers;
|
||||
var timer;
|
||||
var aborted = false;
|
||||
|
||||
// Set User-Agent (required by some servers)
|
||||
// Only set header if it hasn't been set in config
|
||||
@@ -129,7 +128,7 @@ module.exports = function httpAdapter(config) {
|
||||
|
||||
// Create the request
|
||||
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
|
||||
clearTimeout(timer);
|
||||
@@ -177,7 +176,7 @@ module.exports = function httpAdapter(config) {
|
||||
});
|
||||
|
||||
stream.on('error', function handleStreamError(err) {
|
||||
if (aborted) return;
|
||||
if (req.aborted) return;
|
||||
reject(enhanceError(err, config, null, lastRequest));
|
||||
});
|
||||
|
||||
@@ -195,7 +194,7 @@ module.exports = function httpAdapter(config) {
|
||||
|
||||
// Handle errors
|
||||
req.on('error', function handleRequestError(err) {
|
||||
if (aborted) return;
|
||||
if (req.aborted) return;
|
||||
reject(enhanceError(err, config, null, req));
|
||||
});
|
||||
|
||||
@@ -204,20 +203,16 @@ module.exports = function httpAdapter(config) {
|
||||
timer = setTimeout(function handleRequestTimeout() {
|
||||
req.abort();
|
||||
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
|
||||
aborted = true;
|
||||
}, config.timeout);
|
||||
}
|
||||
|
||||
if (config.cancelToken) {
|
||||
// Handle cancellation
|
||||
config.cancelToken.promise.then(function onCanceled(cancel) {
|
||||
if (aborted) {
|
||||
return;
|
||||
}
|
||||
if (req.aborted) return;
|
||||
|
||||
req.abort();
|
||||
reject(cancel);
|
||||
aborted = true;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user