retry-ignore-abort

retry-ignore-abort handles function calls and their potential failure.

Usage no npm install needed!

<script type="module">
  import retryIgnoreAbort from 'https://cdn.skypack.dev/retry-ignore-abort';
</script>

README

retry-ignore-abort

retry-ignore-abort handles function calls and their potential failure.

Status

Category Status
Version npm
Dependencies David
Dev dependencies David
Build GitHub Actions
License GitHub

Installation

$ npm install retry-ignore-abort

Quick Start

First you need to add a reference to retry-ignore-abort to your application:

const { retry, retryIgnoreAbort } = require('retry-ignore-abort');

If you use TypeScript, use the following code instead:

import { retry, retryIgnoreAbort } from 'retry-ignore-abort';

Retrying functions

Sometimes you may want to retry a function in case it fails. For that, use the retry function:

await retry(async () => {
  // ...
});

By default, this performs five retries (i.e., in the worst case the function is run six times), with exponentially increasing timeouts, starting with a delay of 1 second.

If you want to adjust these values, you need to specify an options object with the following properties:

await retry(async () => {
  // ...
}, {
  retries: 5,
  minTimeout: 1_000,
  maxTimeout: 60_000,
  factor: 2
});

If the retried function succeeds and returns a value, this value is returned by the retry function:

const result = await retry(async () => {
  // ...

  return result;
});

In case the function still fails after all retries have been exhausted, an exception is thrown that contains the exception thrown by the inner function in its data property.

Retrying multiple functions

From time to time you have a variety of functions, that need to be run in sequence. In case one of them fails, you may want to retry it, ignore it, or abort the entire sequence. For this, use the retryIgnoreAbort function and hand over an array of functions to run in sequence.

Additionally, you have to provide a function that is called when an exception is thrown. Again, this function may be async, but it does not need to be. The exception that was thrown is handed over as a parameter. Return retry, ignore, or abort to retry, ignore or abort the previous function call:

await retryIgnoreAbort(
  [
    async () => {
      // ...
    },
    async () => {
      // ...
    },
    async () => {
      // ...
    }
  ],
  async ex => {
    // Decide what to do, and return 'retry', 'ignore', or 'abort'.
    return 'ignore';
  }
);

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter