@vitalets/retry
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

@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

Readme

Keywords

none

Package Sidebar

Install

npm i @vitalets/retry

Weekly Downloads

0

Version

1.0.6

License

MIT

Unpacked Size

22.8 kB

Total Files

22

Last publish

Collaborators

  • vitalets