Skip to content

Commit

Permalink
Merge pull request axios#1040 from pbarbiero/pbarbiero/improved-timeo…
Browse files Browse the repository at this point in the history
…ut-handling

Decorate resolve and reject to clear timeout in all cases
  • Loading branch information
nickuraltsev authored Mar 8, 2018
2 parents b14cdd8 + fa3c6d2 commit 4fbf084
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ var enhanceError = require('../core/enhanceError');

/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
return new Promise(function dispatchHttpRequest(resolve, reject) {
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
var timer;
var resolve = function resolve(value) {
clearTimeout(timer);
resolvePromise(value);
};
var reject = function reject(value) {
clearTimeout(timer);
rejectPromise(value);
};
var data = config.data;
var headers = config.headers;
var timer;

// Set User-Agent (required by some servers)
// Only set header if it hasn't been set in config
Expand Down Expand Up @@ -141,10 +149,6 @@ module.exports = function httpAdapter(config) {
var req = transport.request(options, function handleResponse(res) {
if (req.aborted) return;

// Response has been received so kill timer that handles request timeout
clearTimeout(timer);
timer = null;

// uncompress the response body transparently if required
var stream = res;
switch (res.headers['content-encoding']) {
Expand Down Expand Up @@ -210,7 +214,7 @@ module.exports = function httpAdapter(config) {
});

// Handle request timeout
if (config.timeout && !timer) {
if (config.timeout) {
timer = setTimeout(function handleRequestTimeout() {
req.abort();
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
Expand Down

0 comments on commit 4fbf084

Please sign in to comment.