Overview
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
Short Course
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;
}
}
Task
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.
Test Suites
Pending
Next
Output