Make retries recurse backwards

This commit is contained in:
Michal Vaniš 2024-03-11 13:47:31 +01:00
parent 760cc2d718
commit eaabc3aae9

View file

@ -23,7 +23,7 @@ export async function downloadFile(url: string, file: string, options: RequestOp
export function simpleReq( export function simpleReq(
url: string, url: string,
options: RequestOptions = {}, options: RequestOptions = {},
retries = 0, retries = 10,
backoffDelay = 1000 backoffDelay = 1000
): Promise<IncomingMessage> { ): Promise<IncomingMessage> {
return new Promise<IncomingMessage>((resolve, reject) => { return new Promise<IncomingMessage>((resolve, reject) => {
@ -33,10 +33,10 @@ export function simpleReq(
if (statusCode! >= 300) return simpleReq(headers.location!, options).then(resolve).catch(reject); if (statusCode! >= 300) return simpleReq(headers.location!, options).then(resolve).catch(reject);
resolve(res); resolve(res);
}).on("error", err => { }).on("error", err => {
if (retries > 10) { if (retries < 0) {
reject(err); reject(err);
} else { } else {
setTimeout(() => resolve(simpleReq(url, options, retries + 1, backoffDelay * 2)), backoffDelay); setTimeout(() => resolve(simpleReq(url, options, retries - 1, backoffDelay * 2)), backoffDelay);
} }
}); });
}); });