Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@humanwhocodes/retry

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humanwhocodes/retry

A utility to retry failed async methods.

  • 0.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5M
increased by5.99%
Maintainers
1
Weekly downloads
 
Created

What is @humanwhocodes/retry?

@humanwhocodes/retry is an npm package designed to simplify the process of retrying asynchronous operations. It provides a flexible and configurable way to handle retries, making it easier to manage operations that may fail intermittently, such as network requests or database queries.

What are @humanwhocodes/retry's main functionalities?

Basic Retry

This feature allows you to retry a function a specified number of times if it fails. In this example, the `fetchData` function is retried up to 5 times until it succeeds.

const { retry } = require('@humanwhocodes/retry');

async function fetchData() {
  // Simulate a function that may fail
  if (Math.random() < 0.7) {
    throw new Error('Fetch failed');
  }
  return 'Data fetched successfully';
}

retry(fetchData, { retries: 5 })
  .then(result => console.log(result))
  .catch(error => console.error(error));

Exponential Backoff

This feature allows you to implement exponential backoff, where the wait time between retries increases exponentially. In this example, the `fetchData` function is retried up to 5 times with an increasing delay between each retry.

const { retry } = require('@humanwhocodes/retry');

async function fetchData() {
  if (Math.random() < 0.7) {
    throw new Error('Fetch failed');
  }
  return 'Data fetched successfully';
}

retry(fetchData, { retries: 5, factor: 2, minTimeout: 1000 })
  .then(result => console.log(result))
  .catch(error => console.error(error));

Custom Retry Logic

This feature allows you to define custom retry logic by providing a `shouldRetry` function. In this example, the `shouldRetry` function logs each failed attempt and always returns true to continue retrying.

const { retry } = require('@humanwhocodes/retry');

async function fetchData() {
  if (Math.random() < 0.7) {
    throw new Error('Fetch failed');
  }
  return 'Data fetched successfully';
}

retry(fetchData, {
  retries: 5,
  shouldRetry: (error, attempt) => {
    console.log(`Attempt ${attempt} failed: ${error.message}`);
    return true;
  }
})
  .then(result => console.log(result))
  .catch(error => console.error(error));

Other packages similar to @humanwhocodes/retry

Keywords

FAQs

Package last updated on 31 Oct 2024

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