Skip to content

Commit

Permalink
refactor(http): use ClientRequest "aborted" value (#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayrutjes authored and rubennorte committed Jul 10, 2017
1 parent 46e275c commit f1fb3de
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/adapters/http.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});

Expand All @@ -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));
});

Expand All @@ -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;
});
}

Expand Down

0 comments on commit f1fb3de

Please sign in to comment.