npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

retry-client

v1.2.0

Published

retry any function (with or without timeout) with either fixed back off or exponential back off

Downloads

5

Readme

retry-client

codefactor npmSize codecov downloads maintainability

Client that retries any function until no error is thrown and (if desired) function did not timeout. Alternatively, a timeout can be set for a single attempt to resolve the function.

Installation

Use npm to install retry-client.

npm install retry-client

Usage

Retry-client can be used according to its JSDocs definition. Initiate retry-client once and call retry to retry to resolve a function or timeout to try to resolve within timeout period.

const RetryClient = require('retry-client');
const settings = {}; // see below to see available settings
const retryClient = new RetryClient(settings);

new RetryClient(settings)

Initiate RetryClient once. retry and timeout will use the settings provided in RetryClient. The following settings can be passed into the settings object:

  • retryHandler (function) function to be called on retry. Defaults to () => null.
  • errorHandler (function) function to be called on error after last attempt. Defaults to () => null.
  • fixedBackOff (number) fixed back off period (ms). If provided a fixed back off period is used in between retry attempts. Defaults to 0 (no fixed back off)
  • minimumBackOff (number) minimum back off period (ms). This back off period is used on first retry attempt. Defaults to 100.
  • backOffExponent (number) exponent to be used to determine next back off period. Defaults to 1.5.
  • maximumBackOff (number) maximum back off period (ms). Back off period will never exceed the provided value. Defaults to 10000.
  • maximumRetryCount (number) maximum number of retries. Defaults to 5.
  • timeout (number) maximum time allowed to resolve a single iteration (ms). When provided an attempt to resolve functionToCall will be considered as failed if the timeout is exceeded. Defaults to 0 (no timeout).

Both retryHandler and errorHandler will receive the following input when called:

{
  totalDuration: Number, // milliseconds
  iterationDuration: Number, // milliseconds
  iteration: Number, // iteration counter
  remaining: Number, // remaining iterations
  error: Error // details of the error of the regarding iteration
}

retryClient.retry(functionToCall, functionInput)

Attempts to successfully resolve functionToCall with the provided functionInput. Stops retrying when function resolved successfully once.

Example:

const RetryClient = require('retry-client');
const settings = { maximumRetryCount: 3 };
const retryClient = new RetryClient(settings);
const functionToCall = (inputA, inputB, inputC) => {
  //do something
  return `${inputA} - ${inputB} - ${inputC}`;
};
const result = retryClient.retry(functionToCall, ['A', 'B', 'C']);

If successfully resolved within 3 retries, result is: 'A - B - C' Otherwise error of last attempt is thrown.

retryClient.safeRetry(functionToCall, functionInput)

Attempts to successfully resolve functionToCall with the provided functionInput. Stops retrying when function resolved successfully once. Returns null when last attempt failed.

Example:

const RetryClient = require('retry-client');
const settings = { maximumRetryCount: 3 };
const retryClient = new RetryClient(settings);
const functionToCall = (inputA, inputB, inputC) => {
  //do something
  return `${inputA} - ${inputB} - ${inputC}`;
};
const result = retryClient.safeRetry(functionToCall, ['A', 'B', 'C']);

If successfully resolved within 3 retries, result is: 'A - B - C' Otherwise return null.

retryClient.timeout(functionToCall, functionInput)

Attempts to successfully resolve functionToCall with the provided functionInput within the provided timeout (setting).

Example:

const RetryClient = require('retry-client');
const settings = { timeout: 1000 };
const retryClient = new RetryClient(settings);
const functionToCall = (inputA, inputB, inputC) => {
  //do something
  return `${inputA} - ${inputB} - ${inputC}`;
};
const result = retryClient.timeout(functionToCall, ['A', 'B', 'C']);

If successfully resolved within 1000 ms, result is: 'A - B - C' Otherwise the following error is thrown: Timed out after 1000 ms.

retryClient.safeTimeout(functionToCall, functionInput)

Attempts to successfully resolve functionToCall with the provided functionInput within the provided timeout (setting). When not resolved the function returns null.

Example:

const RetryClient = require('retry-client');
const settings = { timeout: 1000 };
const retryClient = new RetryClient(settings);
const functionToCall = (inputA, inputB, inputC) => {
  //do something
  return `${inputA} - ${inputB} - ${inputC}`;
};
const result = retryClient.safeTimeout(functionToCall, ['A', 'B', 'C']);

If successfully resolved within 1000 ms, result is: 'A - B - C' Otherwise return null.