New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

promise-retry-helper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-retry-helper

Utility function to retry Promises

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 21 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc