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

@mrflish/retry

v1.0.6

Published

A typesafe promise based retry pattern

Downloads

2

Readme

Retry

A typesafe promise based retry pattern

retry function

The retry function allows you to quickly retry a promise if it fails. Import it into your project as follows

import retry from "@mrflish/retry";

the retry function takes several parameters the first one is the number of tries before giving up and returning the error that the function threw

const result = await retry(num_of_tries, func_name, [func_param1, func_param2, ...], options);

retry will fail and throw if the function does not resolve before the maximum number of tries reaches its limit.

import retry from "@mrflish/retry";

let count = 1;
const notSureIllResolve = async (param1: string, param2: number, param3: boolean[]) => {
	if(count < 5) 
		throw new Error(`Not yet... [${count++}]`);
	return `About time ! [${count}]`;
};


try {
    //the number of tries is not high enough to allow the function to resolve.
    //retry will throw at the end of the 4th try.
    const MAX_TRIES = 4;
    const result = await retry(MAX_TRIES, notSureIllResolve, ["value of param1", 42, [true, false]]);
    console.log(result);
    
} catch(error: any){
    console.log(error.message);
    //=> "Not yet... [4]"
}

retry will resolve if the function resolves before the maximum number of tries reaches its limit

import retry from "@mrflish/retry";

let count = 1;
const notSureIllResolve = async (param1: string, param2: number, param3: boolean[]) => {
	if(count < 5) {
		throw new Error(`Not yet... [${count++}]`);
	}
	return `About time ! [${count}]`;
};


try {
    //retry will resolve at the 5th try
    //MAX_TRIES is high enough to let the the function resolve
    const MAX_TRIES = 6;
    const result = await retry(MAX_TRIES, notSureIllResolve, ["value of param1", 42, [true, false]]);
    console.log(result);
    //=> "About time ! [5]"
    
} catch(error: any){
    console.log(error.message);
}

Retry class

The Retry class allows you to have more information about the execution of the function Import it into your project as follows

import { Retry } from "@mrflish/retry";

the Retry construcor takes several parameters the first one is the function to try to run

import { Retry } from "@mrflish/retry";
const execute = new Retry(func_name, [func_param1, func_param2, ...], options);
try {
	//the number of tries is not high enough to allow the function to resolve.
	//retry will throw at the end of the 4th try.
	const MAX_TRIES = 4;

    //Will trigger this event each time it tries again
	execute.on("retry", (countDown, currentInterval) => {
		/* Some things to do on each retry here */
	});

    //Will try trigger this event each time it fails
    execute.on("failure", (error, countDown, currentInterval) => {
		/* Some things to do on each failure here */
	});

	const result = await execute.try(MAX_TRIES);

	console.log(result);
		
} catch(error: any){
	console.log(error.message);
	//=> "Not yet... [4]"
}

options

Whether you use the function or the class, you can specify two options:

  • interval

The interval option allows you to specify how long (in milliseconds) to wait before retrying after a failure. Set to 100ms by default

const MAX_TRIES = 4;
const execute = new Retry(notSureIllResolve, ["value of param1", 42, [true, false]], { interval: 500 });

execute.on("retry", (countDown, currentInterval) => {
    console.log(count, currentInterval);
    console.log(`${count} -> ${currentInterval}`);
    /* Some things to do on each retry here */
});

const result = await execute.try(MAX_TRIES);

//OUTPUT
//4 -> 500
//3 -> 500
//2 -> 500
//Not yet... [4]

BinaryExponential

If the binaryExponential option is set to true, each new trial will have its time interval doubled. Set to false by default

const MAX_TRIES = 5;
const execute = new Retry(notSureIllResolve, ["value of param1", 42, [true, false]], { interval: 500, BinaryExponential: true });

execute.on("retry", (countDown, currentInterval) => {
    console.log(count, currentInterval);
    console.log(`${count} -> ${currentInterval}`);
    /* Some things to do on each retry here */
});

const result = await execute.try(MAX_TRIES);

//OUTPUT
//5 -> 500
//4 -> 1000
//3 -> 2000
//2 -> 4000
//About time ! [5]