Retries with exponential backoff
A busy model fails now and then. Retry without hammering the server: 100, 200, 400 ms.
Write withRetry(fn, { retries, baseMs }). Call fn(); if it throws, wait with sleep(ms) and try again. The wait doubles each time: baseMs, baseMs * 2, baseMs * 4… When retries run out, throw the last error.
You already have sleep(ms) and makeFlaky(n), which builds a function that fails n times and then returns 'ok'. Always use sleep: that is how the challenges check the waits.
Challenges 0/4
- Returns 'ok' after two failures
- Waits 100 then 200 ms (doubles)
- Throws once retries run out
- Does not wait when the first call works
async function withRetry(fn, { retries = 3, baseMs = 100 } = {}) {
// try fn(); on error wait sleep(baseMs * 2 ** attempt) and try again
return fn();
}
withRetry(makeFlaky(2), { retries: 3, baseMs: 100 }).then(console.log, (e) => console.error(e.message));Console output appears here (console.log).
Go deeper: the AWS reference →
This in production, with your data? Let's talk for 15 minutes →