Loading article...
Promise retry is a resilience pattern that automatically retries a failed async operation a specified number of times before giving up. This is essential for handling transient network failures, rate limits, or intermittent service unavailability.
function retry(fn, retries = 3, delay = 0) { return new Promise((resolve, reject) => { const attempt = (n) => { fn() .then(resolve) .catch((err) => { if (n === 0) return reject(err); setTimeout(() => attempt(n - 1), delay); }); }; attempt(retries); }); }
let counter = 0; const unstableTask = () => { return new Promise((res, rej) => { counter++; if (counter < 3) rej("fail " + counter); else res("success on attempt " + counter); }); }; retry(unstableTask, 5, 500) .then(console.log) .catch(console.error);
fn is retried up to retries times.delay (ms) between retries.Test your understanding with 3 quick questions