CodeNot attempted
Retry a lazy import
Difficulty: medium
Wrap a dynamic import() so that a failed load is retried with exponential backoff, and the last error is rethrown when every attempt fails. Then explain why this helps with flaky networks but not with a chunk deleted by a deploy.
Show reference solution
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
export async function importWithRetry<T>( load: () => Promise<T>, { retries = 2, baseDelayMs = 500, wait = sleep }: { retries?: number; baseDelayMs?: number; wait?: (ms: number) => Promise<void> } = {},): Promise<T> { for (let attempt = 0; ; attempt++) { try { return await load(); } catch (error) { // Out of retries: let the caller (an error handler that reloads, or an error boundary) deal with it. if (attempt >= retries) throw error; await wait(baseDelayMs * 2 ** attempt); } }}
Ctrl Enter