diff --git a/src/utils/http.js b/src/utils/http.js index 32e9a8b4..4573f833 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -80,7 +80,7 @@ function getLimiter(options = {}, url) { return limiters[interval][concurrency]; } -async function request(method = 'get', url, body, requestOptions = {}, limiter) { +async function request(method = 'get', url, body, requestOptions = {}, limiter, timeout) { const http = requestOptions.session || bhttp; const options = { @@ -107,7 +107,7 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter) ? http[method](url, body, options) : http[method](url, options)); - const resIsOk = res.statusCode >= 200 && res.statusCode <= 299; + timeout.cancel(); if (options.destination) { // res.on('progress', (bytes, totalBytes) => logger.silly(`Downloaded ${Math.round((bytes / totalBytes) * 100)}% of ${url}`)); @@ -126,7 +126,7 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter) status: res.statusCode, document: window.document, window, - ok: resIsOk, + ok: res.statusCode >= 200 && res.statusCode <= 299, }; } @@ -152,9 +152,7 @@ async function scheduleRequest(method = 'get', url, body, options) { const limiter = getLimiter(options, url); const timeout = getTimeout(options, url); - const result = await limiter.schedule(() => Promise.race([request(method, url, body, options, limiter), timeout])); - - timeout.cancel(); + const result = await limiter.schedule(() => Promise.race([request(method, url, body, options, limiter, timeout), timeout])); return result; }