MediumPro challengePythonJavaScriptTypeScript

Retry with Exponential Backoff

Node.jsAsyncRetry

solve(scenarios, maxAttempts) retries an async fn up to maxAttempts times.
Returns the result. If all attempts fail, throws the last error.

scenarios = an array of strings: 'fail' or 'ok:<value>'.
The fn consumes one scenario per call.

Sample tests

Test #1Succeeds first try
Input: [["ok:hello"],3]
Output: {"ok":true,"value":"hello"}
Test #2Succeeds on 3rd
Input: [["fail","fail","ok:hi"],3]
Output: {"ok":true,"value":"hi"}
Test #3All fail
Input: [["fail","fail","fail"],3]
Output: {"ok":false,"error":"failed"}
Test #42nd attempt OK
Input: [["fail","ok:done"],2]
Output: {"ok":true,"value":"done"}
Test #5Single attempt fails
Input: [["fail"],1]
Output: {"ok":false,"error":"failed"}