import { sleep } from "./promises" ; type StopConditionType = ( tryIdx : number ) => boolean ; param url param maxRetries param delay param stopCondition returns export const fetchRetry = async ( url , maxRetries = 5 , delay = 200 , shouldStop = ( ) => false , timeout = false ) : Promise < Response > => { let error ; for ( let i = 0 ; i < maxRetries ; i ++ ) { if ( shouldStop ( i ) ) break ; try { const controller = new AbortController ( ) ; if ( timeout !== false ) { setTimeout ( ( ) => controller . abort ( ) , timeout ) ; } const data = await fetch ( url , { signal : controller . signal } ) ; if ( data . status === 404 ) throw "404" ; return data ; } catch ( err ) { error = err ; } await sleep ( delay ) ; } throw error ; } ;