import { lazy , ComponentType , LazyExoticComponent } from 'react' ; const DEFAULT_RETRIES = 5 ; const DEFAULT_INTERVAL = 1000 ; param componentImport param retriesLeft param interval const wait = ( interval ) => new Promise ( resolve => setTimeout ( resolve , interval ) ) ; const retry = async < T > ( componentImport , retriesLeft = DEFAULT_RETRIES , interval = DEFAULT_INTERVAL ) : Promise < { default : ComponentType < T > ; } > => { try { return componentImport ( ) ; } catch ( error ) { await wait ( interval ) ; if ( retriesLeft === 0 ) { throw error instanceof Error ? error : new Error ( ) ; } return retry ( componentImport , retriesLeft - 1 , interval ) ; } } ; export const lazyWithRetry = < T > ( componentImport , retriesLeft = DEFAULT_RETRIES , interval = DEFAULT_INTERVAL ) : LazyExoticComponent < ComponentType < T > > => lazy ( ( ) => retry ( componentImport , retriesLeft , interval ) ) ; export default lazyWithRetry ;