A simple utility to retry a function multiple times until it succeeds or reaches the maximum number of attempts.
npm install @coxy/replay-count-loop
or
yarn add @coxy/replay-count-loop
const random = () => {
if (Math.random() > 0.01) {
throw new Error(':(');
}
return 'Success!';
}
import replayCountLoop from '@coxy/replay-count-loop';
const loop = replayCountLoop(random, {
onError: e => console.log('error', e.message),
attempts: 20,
delay: 100,
});
loop.then(console.log);
loop.catch(console.error);
-
attempts
(number): Maximum number of retry attempts (default: 10) -
delay
(number): Delay in milliseconds between attempts (default: 0) -
onError
(function): Optional callback executed after each failed attempt
- Lightweight and minimalistic.
- Supports customizable retry logic.
- Promise-based API.
Fun fact: The concept of retrying operations is heavily used in network programming, where transient failures are common!