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

retry-as-promised

Package Overview
Dependencies
Maintainers
0
Versions
25
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.1.1
latest
Source
npm
Version published
Maintainers
0
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

retry

FAQs

Package last updated on 27 Feb 2025

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