@coxy/reply-count-loop
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@coxy/replay-count-loop

A simple utility to retry a function multiple times until it succeeds or reaches the maximum number of attempts.

Installation

npm install @coxy/replay-count-loop

or

yarn add @coxy/replay-count-loop

Usage

Register a function

const random = () => {
  if (Math.random() > 0.01) {
    throw new Error(':(');
  }
  return 'Success!';
}

Add to loop

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);

Options

  • 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

Features

  • 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!

Readme

Keywords

Package Sidebar

Install

npm i @coxy/reply-count-loop

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

8.19 kB

Total Files

12

Last publish

Collaborators

  • dsshard