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

rolly-retry

v2.1.6

Published

Retries one or more functions and consecutively checks for success.

Downloads

3

Readme

rolly-retry

A retry, failover library with types.

State

npm version npm downloads

license

build coverage coverage coverage

Motivation

  1. I needed a way to call a function n times until the call succeeds.
  2. I needed to be able to define a successful call.
  3. I wanted to try calling 1 or more different functions until one succeeded.

Usage

retry one async function

import { retry } from "rolly-retry";

/**
* call retriable_func("value")
* until the result === "success"
* up to 4 times
*/
await retry(
  async () => await retriable_func("value"), 
  (result) => result === "success",
  4
);

retry and failover

import { retry } from "rolly-retry";

/**
* call primary_retriable()
* until the result === "success"
* up to 4 times
* otherwise, call backup_retriable()
* until the result === "success"
* up to 4 times
*/
await retry(
  [
    async () => await primary_retriable(),
    async () => await backup_retriable(),
  ],
  (result) => result === "success",
  4
);

retrying with backoff

You can pass a back off to the retry. The back off will cause a delay after the first retry. Backoffs reset for each function.

import { retry } from "rolly-retry";

await retry(
  [
    async () => await func_i_want_to_try(1)
  ], 
  (result) => false,
  3,
  { 
    constant: 10, // constant 10ms delay after first try
    linear: 20    // linear X20ms delay after first try
  }
);
expect(calls).toBe(3);
/**      
 * Call  Constant  Linear  Delay TotalMs
 * 0:    0         0       0     0
 * 1:    10        20      30    30
 * 2:    10        40      50    80
*/

synchronous

import { retry } from "rolly-retry";

let value = "";
await retry(() => (value += "a"), (result) => result === "aaa", 4);
expect(value).toBe("aaa");

Install

npm i rolly-retry

Avoid

Unrelated calls

Function calls within a retry(func | [funcs]) operation are essentially synchronous. If you want to asynchronously retry many unrelated functions start multiple retries. e.g.,

import { retry } from "rolly-retry";

const retryOne = retry(funcGroupOne, successOne, 2);
const retryTwo = retry(funcGroupTwo, successTwo, 2);
const allResults = await Promise.allSettled([retryOne, retryTwo]);