promise-retry-helper
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

codecov version MIT

promise-retry-helper

Small utility function to retry a Promise.
If this function does not meet your needs, there are other packages in npm registry that you can explore.

Installation

This package can be used in node and browser environments.

Node.js

Install package via npm

$ npm install promise-retry-helper

or via yarn

$ yarn add promise-retry-helper

Browser

Simply load ./dist/promise-retry-helper.min.js via script tag.

<script src="./dist/promise-retry-helper.min.js"></script>

The function will be available globally under the name promiseRetryHelper.

Usage

retry(input: Function, options?: object)

Calls input function and waits until the returned Promise ends up fulfilled or rejected. If Promise is rejected, it will attempt to retry the input function.

input (required) - Function to execute. It should return a Promise.
options (optional) - Configuration object:

  • retries (optional, default: 3) - Maximum amount of times to retry the input function.
  • delay (optional, default: 1000) - Number of milliseconds between each retry.

When you want to trigger a retry, you can just reject a Promise in input function.

Example

const retry = require('promise-retry-helper');

retry(() => {
  const randomId = Math.floor(Math.random() * 2);
  return fetch(`https://jsonplaceholder.typicode.com/todos/${randomId}`)
    .then(response => {
      if (response.ok) {
        // Success, retry will not be triggered
        return response;
      }

      // Trigger retry
      return Promise.reject(response);
    });
})
  .then((response) => {
    // Handle success
  })
  .catch((error) => {
    // Handle error
  });

Using with async/await

const retry = require('promise-retry-helper');

const inputFunction = async () => {
  const randomId = Math.floor(Math.random() * 2);

  const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${randomId}`);

  if (response.ok) {
    // Success, retry will not be triggered
    return response.json();
  }

  // Trigger retry
  throw response
}

try {
  const data = await retry(inputFunction, { retries: 2, delay: 500 });
  // Handle success
} catch(error) {
  // Handle error
}

License

Released under the MIT License.

Package Sidebar

Install

npm i promise-retry-helper

Weekly Downloads

5

Version

1.2.0

License

MIT

Unpacked Size

26.4 kB

Total Files

6

Last publish

Collaborators

  • rmmgc