Retry asynchronous operations
This package is distributed via npm:
npm install @antoniovdlc/with-retry
You can use this library either as an ES module or a CommonJS package:
import withRetry from "@antoniovdlc/with-retry";
- or -
const withRetry = require("@antoniovdlc/with-retry");
You can then wrap any asynchronous function with withRetry
and pass configuration options:
async function fn() { ... }
const result = await withRetry(fn, { ... });
The configuration options are:
type RetryConfig = {
maxAttempts: number;
delay: number;
maxDelay: number;
backoffStrategy: RetryConfigBackoffStrategy;
jitter: RetryConfigJitter;
retryCondition: (error: Error) => boolean;
onRetry: (error: Error, attempt: number) => void;
onExhausted: (error: Error) => void;
timeout: number;
};
Default values are set to:
{
maxAttempts: 3,
delay: 100,
maxDelay: 1000,
backoffStrategy: "constant",
jitter: true,
retryCondition: () => true,
onRetry: () => {},
onExhausted: () => {},
timeout: 0,
}
MIT