All academies
All challenges

Generic Retry with Async Error Handling

hardasync200 XP

Problem

Write retry<T>(operation: () => Promise<T>, attempts: number): Promise<T> that calls the operation up to the given number of times. On each failure, log "attempt N failed" and try again; when an attempt succeeds, return its result; if all attempts fail, rethrow the last error. Test it with a flaky operation that rejects on its first two calls and resolves on the third.

Expected output

attempt 1 failed
attempt 2 failed
success on attempt 3

Hints

Stuck? Reveal hints one at a time.

Solution

Try solving it yourself first — revealing is always available.

Press Run in the editor — your console output is checked automatically.
No errors
Loading...

Console output appears here. Press Run or just type — the code compiles automatically.