1.0.6 • Published 2 years ago

@vitalets/retry v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago