mirror of
https://github.com/tenrok/axios.git
synced 2026-06-02 16:04:10 +03:00
Merge pull request #1040 from pbarbiero/pbarbiero/improved-timeout-handling
Decorate resolve and reject to clear timeout in all cases
This commit is contained in:
+11
-7
@@ -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
|
||||
@@ -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']) {
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user