JavaScript · Level 3 Project

Retry with Backoff

Write the helper that real apps wrap around unreliable network calls: try again after a short, growing pause, and give up with the last error.

  • try / catch around await
  • Waiting between attempts
  • Rethrowing the last error

try/catch only catches a rejected Promise if you await it inside the try. Trap: return fn() without await lets the error escape your catch; write return await fn().

async function safe(fn) {
  try {
    return await fn();    // the await makes catch work
  } catch (err) {
    return "failed: " + err.message;
  }
}

Write async function retry(fn, times): await fn() up to times attempts, waiting 5 ms after the first failure and doubling each time; return the first success, or throw the last error, e.g. retry(broken, 3) throws boom 3.

solution.js
Test Suites Pending
Output