MediumPro challengePythonJavaScriptTypeScript

Async Retry

AsyncPromisesPatterns

Implement retry(fn, maxAttempts) that calls fn() up to maxAttempts
times. Return the first successful result. If all attempts fail, throw the
last error.

solve(failCount, value) creates a function that rejects the first
failCount times then resolves with value. It returns {result, attempts}.

Sample tests

Test #1Succeeds on first attempt
Input: [0,"ok",3]
Output: {"result":"ok","attempts":1}
Test #2Fails twice, succeeds on third
Input: [2,"ok",3]
Output: {"result":"ok","attempts":3}
Test #3Always fails within budget → null result
Input: [5,"ok",3]
Output: {"result":null,"attempts":3}
Test #4Fails once, succeeds second time
Input: [1,42,5]
Output: {"result":42,"attempts":2}
Test #5One attempt, immediate success
Input: [0,"first",1]
Output: {"result":"first","attempts":1}