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

re

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

re

Do it again, after a bit.

  • 0.0.1-1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3K
increased by35.76%
Maintainers
1
Weekly downloads
 
Created
Source

Re

Do it again, if it doesn't work the first time. Supports various configurable retry strategies, including: constant, exponential backoff and linear backoff. In the style of the async library.

Install

  npm install re

Usage

If you like the defaults, call it like this:

var retry = require('re'),
    re = retry.Re();

re.try(function(retryCount, fail, callback){
    if(retryCount < 2) fail(new Error("Not there yet!"));
    else callback(retryCount);
  },
  function(retryCount){
    console.log("It took this many tries: " + retryCount);
});

The re.try function takes two arguments, a function to call until it works (or we run out of retries) and a function to call when it finally succeeds (or we fail too many times). As the name suggests we automatically wrap your function in a standard try block and, if an exception occurs, call it again according to the retry schedule.

This first function should take 3 arguments like this:

function operation(retryCount, fail, callback)

The retryCount argument is the number if the current retry, it'll be zero the first time and get bigger every time. The fail argument is a function to call if you encounter a fail condition in your function. This let's us know we need to try again. You can pass an err argument to the fail function. The callback argument is the callback function you passed into re.try. It should be a function that takes an err parameter as it's first argument. The rest of the arguments are up to you. Call this when you succeed. We'll call it with the last exception or whatever you passed to the last fail, when too many failures happen.

Options

The default options look like this:

var options = {
    retries : 4,
    retryStrategy : {"type": RETRY_STRATEGY.EXPONENTIAL, "multiplier":300, "base":3},
}

You pass this into the Re constructor.

var re = retry.Re(options);

This gives you 4 retries and an exponential backoff strategy with the following progression (in milliseconds): 300, 900, 2700, 8100.

Retry Strategy Examples

The following will retry every 400 milliseconds:

{"type": RETRY_STRATEGY.CONSTANT, "constant": 400}

The following will give a linear backoff strategy that has the following progress (when paired with retries: 4) : 200, 400, 600, 800

{"type": RETRY_STRATEGY.LINEAR, "multiplier": 200}

FAQs

Package last updated on 28 Nov 2012

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