Skip to main content

Retry Strategy

Code sample of retry logic

async function requestWithRetry(fn, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
const shouldRetry =
error.status >= 500 ||
error.code === 'RATE_LIMIT_EXCEEDED';

if (!shouldRetry || attempt === maxRetries) {
throw error;
}

const delay = Math.pow(2, attempt) * 1000;
await new Promise(r => setTimeout(r, delay));
}
}
}