Skip to the solution
MediumFrontend4 min read

Implement promiseRetry(fn, retries, delay) with exponential backoff

1.2k views

The problem

Problem, as an interviewer would state it: "Write promiseRetry(fn, retries, delayMs) — it calls fn, and if it rejects, waits and tries again, up to retries additional times, doubling the delay each time (exponential backoff)."

Examples:

Code
await promiseRetry(flakyFn, 3, 100); // retries up to 3 more times, delays 100/200/400ms

Clarifying questions expected:

  • Does retries mean total attempts, or additional attempts AFTER the first real try?
  • Should the delay genuinely double each time (exponential), or is a fixed delay acceptable?
  • Is there a maximum total delay/timeout across all retries combined, or unbounded?

Code / implementation expected: Yes — real, direct proof of both outcomes: a function that fails its first 2 attempts then succeeds, and a function that genuinely always fails, exhausting all real retries before throwing.

  • retry
  • promise
  • exponential-backoff

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: promiseRetry() correctly retries a function that eventually succeeds, and exhausts the exact configured retry count on one that always fails

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 80 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a Minimal createStore() (Redux-Lite: dispatch, subscribe, reducers)

Continue