Explorer
Node.js

Write `promiseRetry(fn, retries, delay)` that retries an async function up to retries times with a delay between attempts.

Problem Statement

Write `promiseRetry(fn, retries, delay)` that retries an async function up to retries times with a delay between attempts.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: If fn() resolves, return the result. If it rejects, retry. šŸ’” Hint 2: Use recursion or a loop with await to retry. šŸ’” Hint 3: Decrement retries on each failure. When retries hits 0, reject. āœ… Solution: ```javascript async function promiseRetry(fn, retries, delay) { try { return await fn(); } catch (err) { if (retries <= 0) throw err; await new Promise(r => setTimeout(r, delay)); return promiseRetry(fn, retries - 1, delay); } } ```

Implement Promise.retry

Hard
Write `promiseRetry(fn, retries, delay)` that retries an async function up to retries times with a delay between attempts.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
"success"