I gave it a swing with deno and came up with this:
fetchWithRetry.ts
typeParameters={url:stringnumberOfRetries?:numberprint?:booleandev?:boolean}exportasyncfunctionfetchWithRetry({url,numberOfRetries=3,print,dev,}:Parameters):Promise<Response>{constretriesLeft=numberOfRetries-1print&&console.log(`Retries left: ${retriesLeft}, url: ${url}`)if(retriesLeft==0)returnPromise.reject('Too many attemps without luck')returnawaitfetch(url).catch(async(error)=>{if(numberOfRetries!==0){awaitfetchWithRetry({url:!dev?url:url.slice(0,-1),numberOfRetries:retriesLeft,print,dev,})}returnerror})}if(import.meta.main){console.log('main')constsuccessUrl='https://www.google.com'constfailureUrl='https://www.googsdsdsdsdle.com'constpartialFailingUrl='https://www.google.com1'console.log(successUrl)awaitfetchWithRetry({url:successUrl,print:true})console.log(failureUrl)awaitfetchWithRetry({url:failureUrl,print:true}).catch(()=>{})console.log(partialFailingUrl)awaitfetchWithRetry({url:partialFailingUrl,print:true,dev:true})}
deno run --allow-net fetchWithRetry.ts
The short version of it
typeParameters={url:stringnumberOfRetries?:number}exportasyncfunctionfetchWithRetry({url,numberOfRetries=3}:Parameters):Promise<Response>{constretriesLeft=numberOfRetries-1if(retriesLeft==0)returnPromise.reject('Too many attemps without luck')returnawaitfetch(url).catch(async(error)=>{if(numberOfRetries!==0){awaitfetchWithRetry({url,numberOfRetries:retriesLeft,})}returnerror})}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I gave it a swing with deno and came up with this:
The short version of it