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

retry-as-promised

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

retry-as-promised

Retry a failed promise

  • 7.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9M
increased by5.47%
Maintainers
1
Weekly downloads
 
Created

What is retry-as-promised?

The retry-as-promised npm package allows you to retry a promise-returning function a specified number of times with a delay between each attempt. This is useful for handling transient errors in asynchronous operations, such as network requests or database queries.

What are retry-as-promised's main functionalities?

Basic Retry

This feature allows you to retry a promise-returning function up to a specified number of times (in this case, 3) with a delay between each attempt (in this case, 1000 milliseconds).

const retry = require('retry-as-promised');

const myFunction = () => {
  return new Promise((resolve, reject) => {
    // Simulate an operation that may fail
    if (Math.random() > 0.5) {
      resolve('Success');
    } else {
      reject('Failure');
    }
  });
};

retry(() => myFunction(), { max: 3, timeout: 1000 })
  .then(result => console.log(result))
  .catch(err => console.error(err));

Custom Retry Logic

This feature allows you to customize the retry logic, including the number of retries, the delay between retries, and the conditions under which to retry. In this example, the function will retry up to 5 times with an increasing delay based on the backoffBase and backoffExponent.

const retry = require('retry-as-promised');

const myFunction = () => {
  return new Promise((resolve, reject) => {
    // Simulate an operation that may fail
    if (Math.random() > 0.5) {
      resolve('Success');
    } else {
      reject('Failure');
    }
  });
};

retry(() => myFunction(), {
  max: 5,
  timeout: 2000,
  match: [/Failure/],
  backoffBase: 1000,
  backoffExponent: 1.5
})
  .then(result => console.log(result))
  .catch(err => console.error(err));

Retry with Custom Error Handling

This feature allows you to specify custom error handling logic for retries. In this example, the function will retry up to 4 times with a delay that doubles each time, but only if the error message matches 'Custom Error'.

const retry = require('retry-as-promised');

const myFunction = () => {
  return new Promise((resolve, reject) => {
    // Simulate an operation that may fail
    if (Math.random() > 0.5) {
      resolve('Success');
    } else {
      reject(new Error('Custom Error'));
    }
  });
};

retry(() => myFunction(), {
  max: 4,
  timeout: 1500,
  match: [err => err.message === 'Custom Error'],
  backoffBase: 500,
  backoffExponent: 2
})
  .then(result => console.log(result))
  .catch(err => console.error(err));

Other packages similar to retry-as-promised

Keywords

FAQs

Package last updated on 07 Feb 2023

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