@vitalets/retry
v1.0.6
Published
Retry async function with exponential delay, timeouts and abort signals.
Downloads
12
Readme
@vitalets/retry
Retry async function with exponential delay, timeouts and abort signals.
Features
- exponential delays
- exponential timeouts
- abort signals
Usage
import { withRetry, RetryFn } from '@vitalets/retry';
const fn: RetryFn = async ({ signal }) => {
const res = await fetch('https://example.com', { signal });
if (!res.ok) throw new Error(`Status ${res.status} ${await res.text()}`);
return res.json();
};
const result = await withRetry(fn, {
retries: 5,
delay: [ 100, 200, 500 ], // or exponential { min: 100, factor: 2, max: 1000 }
timeout: [ 300, 400, 1000 ], // or exponential { min: 300, factor: 2, max: 1000 }
onBeforeRetry: ({ e, retry, timeout }) => {
console.log(`${e?.message}. Retry #${retry} with timeout ${timeout}ms`);
},
});
Installation
npm i @vitalets/retry
Note: this package is ESM only
Options
tbd
Related
License
MIT @ Vitaliy Potapov