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

tiny-retry

v1.6.5

Published

Lightweight function to retry an async job until the job success or stop after a maximum number of tries

Downloads

89

Readme

Tiny retry JS 👷

npm npm bundle size npm GitHub

A lightweight module (~0.5kb ✨) to retry an async job until the job success or stop after a maximum number of tries

package size

Usage

  • Install package

    npm install tiny-retry
    # or yarn add tiny-retry
  • Use it

    import retry from "tiny-retry";
    
    // Async context
    const result = await retry(asyncJob, options);
    
    if (result.success) {
    	// Do something with job data
    	console.log(result.data)
    } else {
    	// Do other thing
    }

Parameters

const result = await retry(asyncJob, { maxTries, delay, startAfter, process, errorHandler, check });
  • asyncJob [Function]: async function that throw an Error if failed
  • options [Object]: Retry options
    • options.maxTries [Number]: number of maximum time to try to run job
    • options.delay [Number]: the number in miliseconds of time after between each tries
    • options.starAfter [Number]: the number in miliseconds to start the 1st try
    • options.process [Function]: A process function to run before each try with the tries count argument
    • options.errorHandler [Function]: A function to handle error in each try with the err argument
    • options.check [Function]: A function with the job reponse argument to verify whether the job response is expected or not (throw an Error if not)

Return value

  • result [Object]: Represent the value of the job done or not and include job's data (if job return)
    • result.success [Boolean]: Whether the job success or not
    • result.tries [Number]: Number of tries
    • result.[data] [Number]: The return value of job if success
console.log(result)

// Expect: { success: true, data: "Async job data", tries }
// If job failed: { success: false, tries }

Example

Codesandbox Example

import retry from "tiny-retry";

const wait = (secs) => new Promise((resolve) => setTimeout(resolve, secs));

let count = 0;
const fakeJobThatDoneAfter6Tries = async () => {
	await wait(2000);
	count += 1;
	if (count < 4) {
		throw new Error('Job failed!');
	} else if (count < 6) {
		return false
	} else {
		console.log('Job done!');
		return "Job data"
	}
};

(async () => {
	console.log("Start Job");
	console.time("JOB_COST");
	const result = await retry(fakeJobThatDoneAfter6Tries, {
		process: (tries) => console.log(`[TRY]: ${tries} time(s)`),
		errorHandler: (err) => console.log(err.toString()),
		check: Boolean,
		maxTries: 10,
		delay: 1000,
		startAfter: 0
	});
	console.log("Job result: ", result);
	console.log("Time expect: 0 + 2*6 + 1*(6-1) = 17s");
	console.timeEnd("JOB_COST"); // Expect 17s
})();

License

Copyright (c) 2021 Leo Huynh @ https://leohuynh.dev under MIT LICENSE