retry-await
v1.0.0
Published
Lightweight async retry with exponential backoff, jitter, and abort support
Maintainers
Readme
retry-await
Lightweight async retry with exponential backoff, jitter, and abort support. Zero dependencies.
Install
npm install retry-awaitUsage
const retry = require('retry-await');
// Basic
const data = await retry(() => fetch('https://api.example.com/data'));
// With options
const result = await retry(
async (attempt) => {
console.log(`Attempt ${attempt + 1}`);
const res = await fetch('https://api.example.com/data');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
},
{
retries: 5,
minTimeout: 500,
maxTimeout: 10000,
factor: 2,
jitter: true,
onRetry: (err, attempt) => console.warn(`Retry #${attempt}: ${err.message}`),
}
);
// With AbortController
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
const data = await retry(() => fetch(url), { signal: controller.signal });API
retry(fn, options?)
fn(attempt)— async function to retry. Receives zero-based attempt number.options.retries— max retries (default: 3)options.minTimeout— initial delay in ms (default: 1000)options.maxTimeout— max delay in ms (default: 30000)options.factor— exponential factor (default: 2)options.jitter— randomize delay (default: true)options.onRetry(err, attempt)— callback on each retryoptions.signal— AbortSignal to cancel retries
License
MIT
