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

async-retry

Package Overview
Dependencies
Maintainers
80
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-retry

Retrying made simple, easy and async

  • 1.3.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9M
increased by0.9%
Maintainers
80
Weekly downloads
 
Created

What is async-retry?

The async-retry npm package is designed to handle the execution of asynchronous operations that may fail by retrying them a specified number of times with customizable options. It is useful for dealing with operations like network requests or any other tasks that might intermittently fail due to temporary issues.

What are async-retry's main functionalities?

Basic retry functionality

This feature allows you to automatically retry an asynchronous operation if it fails. The code sample shows how to use async-retry to fetch data from an API with up to 5 retries.

const retry = require('async-retry');

async function fetchData() {
  return retry(async bail => {
    // if anything throws, we retry
    const result = await fetch('https://api.example.com/data');
    if (result.status >= 400) {
      throw new Error('Request failed');
    }
    return result.json();
  }, {
    retries: 5
  });
}

Custom retry strategies

This feature allows you to define a custom retry strategy, including the number of retries, the factor by which the timeout increases, and the minimum and maximum timeout values. The code sample demonstrates setting these options for a more sophisticated retry strategy.

const retry = require('async-retry');

async function fetchDataWithCustomStrategy() {
  return retry(async bail => {
    // if anything throws, we retry
    const result = await fetch('https://api.example.com/data');
    if (result.status >= 400) {
      throw new Error('Request failed');
    }
    return result.json();
  }, {
    retries: 5,
    factor: 2,
    minTimeout: 1000,
    maxTimeout: 5000,
    randomize: true
  });
}

Bailing out of retries

This feature allows you to bail out of the retry loop early under certain conditions. The code sample shows how to stop retrying if a 404 status is encountered or after a certain number of attempts.

const retry = require('async-retry');

async function fetchDataWithBail() {
  return retry(async (bail, attempt) => {
    try {
      const result = await fetch('https://api.example.com/data');
      if (result.status === 404) {
        // Don't retry upon 404
        bail(new Error('Not found'));
        return;
      }
      return result.json();
    } catch (error) {
      if (attempt >= 3) {
        // Bail on the third attempt
        bail(error);
        return;
      }
      throw error;
    }
  }, {
    retries: 5
  });
}

Other packages similar to async-retry

FAQs

Package last updated on 17 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