3.0.0 • Published 7 months ago

@chriscdn/promise-retry v3.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 months ago

@chriscdn/promise-retry

Retry an asynchronous function until it resolves successfully or exceeds the maximum attempt count.

Installing

Using npm:

npm install @chriscdn/promise-retry

Using yarn:

yarn add @chriscdn/promise-retry

Upgrading to v3

The import has changed from

import promiseRetry from "@chriscdn/promise-retry";

to

import { promiseRetry } from "@chriscdn/promise-retry";

Example 1 - Async/Await

import { promiseRetry, type RetryOptions } from "@chriscdn/promise-retry";

// all options are optional, defaults below
const options: RetryOptions = {
  maxAttempts: 10, // Maximum retry attempts (default: 10)
  retryDelay: 0, // Delay between retries (in ms)
  onError: (err, attempt) => {
    // log the error
  },
};

const results = await promiseRetry(async (attempt) => {
  // do something async in here
}, options);

Example 2 - Retryify

Retryify wraps an asynchronous function and returns a new function with the same interface. If the original function fails (i.e., rejects its promise), it will automatically retry the function up to the specified number of times before rejecting.

import { Retryify } from "@chriscdn/promise-retry";

const myAsyncFunctionRandomFails = async (a, b) => {
  if (Math.random() < 0.2) {
    return a + b;
  } else {
    throw new Error("Random failure");
  }
};

const myAsyncFunctionRetry = Retryify(myAsyncFunctionRandomFails, options);

try {
  const sum = await myAsyncFunctionRetry(1, 5);
  console.log("Success: ", sum);
} catch (err) {
  console.error("Failed after retries:", err);
}

License

MIT

3.0.0

7 months ago

2.0.7

1 year ago

2.0.6

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

1.0.2

4 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

5 years ago

1.0.0

6 years ago