Skip to content

Type Alias: RetryOptions ​

ts
type RetryOptions = object;

Options for retrying a function.

Example ​

ts
import { Utils } from '@heliomarpm/helpers';

const unreliableFunction = () => {
  if (Math.random() < 0.7) {
    throw new Error('Random failure');
  }
  return 'Success';
};

const result = await Utils.retry(unreliableFunction, {
  retries: 5,
  delay: 2000,
  onRetry: (error, attempt) => {
    console.log(`Attempt ${attempt} failed: ${error.message}`);
  }
});

console.log(result); // 'Success' (if successful within the retry limit)

Properties ​

delay? ​

ts
optional delay: number;

The delay in milliseconds between retries.


onRetry()? ​

ts
optional onRetry: (error, attempt) => void;

A callback function that is called before each retry.

Parameters ​

error ​

Error

attempt ​

number

Returns ​

void


retries? ​

ts
optional retries: number;

The number of times to retry the function.