intermediate
Review: the retry that quits one attempt early
This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.
The client wrapper around a flaky upstream ships with a clear contract: `retries` is the number of additional attempts after the first — so `retries: 2` means up to three calls, and `retries: 0` still makes its one attempt.
export interface RetryOptions {
retries: number
baseDelayMs: number
}
/**
* Calls `load`, retrying up to `options.retries` additional times with
* exponential backoff. Rejects with the last error from `load` when every
* attempt fails. `sleep` is injected for testing.
*/
export async function fetchWithRetry<T>(
load: () => Promise<T>,
options: RetryOptions,
sleep: (ms: number) => Promise<void>
): Promise<T> {
for (let attempt = 0; attempt < options.retries; attempt++) {
try {
return await load()
} catch {
if (attempt < options.retries - 1) {
await sleep(options.baseDelayMs * 2 ** attempt)
}
}
}
throw new Error(`gave up after ${options.retries} attempt(s)`)
}review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code