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-catch

v1.0.2

Published

A simple higher-order function allowing execution to be repeated until a condition is satisfied or a limit is reached.

Downloads

3

Readme

What is this?

A simple higher-order function allowing execution to be repeated until a condition is satisfied or a limit is reached.

How do I install it?

npm install retry-catch

How can I use it?

Basic usage

const {retryCatchable} = require('retry-catch');

const fnWithChanceOfFailing = (chanceOfFailing = 0.8) => {
  return new Promise((resolve, reject) => {
    if (Math.random() < chanceOfFailing) {
      reject('KO');
      return;
    }
    resolve('OK');
  });
};

const retryFn = retryCatchable(fnWithChanceOfFailing, {
  retries: 5,
  delay: 1000,
  backoff: 2,
});

retryFn(0.8)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });

The function will randomly fail 80% of the time. The retry function will retry the function 5 times, with a delay of 1 second between each retry. The delay will be multiplied by 2 after each retry.
The function will resolve with the result of the function if it succeeds, or reject with the error if it fails after all retries.

Advanced usage - shouldRetryOnReject

You can also retry on reject, if you want to retry only if a specific error is returned.

const {retryCatchable} = require('retry-catch');

const fnWithChanceOfFailing = (chanceOfFailing = 0.8) => {
  return new Promise((resolve, reject) => {
    if (Math.random() < chanceOfFailing) {
      reject(Math.random() < 0.8 ? 'KO' : 'REAL-KO');
      return;
    }
    resolve('OK');
  });
};

const retryFn = retryCatchable(fnWithChanceOfFailing, {
  retries: 5,
  delay: 1000,
  backoff: 2,
  shouldRetryOnReject: (error) => {
    return error === 'KO';
  },
});

retryFn(0.8)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });

The function will randomly fail 80% of the time. The retry function will retry the function 5 times, with a delay of 1 second between each retry. The delay will be multiplied by 2 after each retry.
The function will not retry if the error is not equal to 'KO'.

Advanced usage - shouldRetryOnResolve

You can also retry on resolve, if you want to retry only if a specific result is returned.

const {retryCatchable} = require('retry-catch');

const fnWithChanceOfFailing = (chanceOfFailing = 0.8) => {
  return new Promise((resolve, reject) => {
    if (Math.random() < chanceOfFailing) {
      reject('KO');
      return;
    }
    resolve(Math.random() < 0.8 ? 'OK' : 'REAL-OK');
  });
};

const retryFn = retryCatchable(fnWithChanceOfFailing, {
  retries: 5,
  delay: 1000,
  backoff: 2,
  shouldRetryOnResolve: (result) => {
    return result === 'OK';
  },
});

retryFn(0.8)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });

The function will randomly fail 80% of the time. The retry function will retry the function 5 times, with a delay of 1 second between each retry. The delay will be multiplied by 2 after each retry.
The function will not retry if the result is not equal to 'OK'.

API

The retryCatchable function takes 2 parameters:

  • The function to retry (the function must return a promise).
  • An object containing the list of options. You can pass the following properties and methods, all of them are optional:
    • retries: The number of retries to attempt. Default: 3.
    • delay: The delay between every retry. Default: 1000.
    • backoff: The backoff multiplier. Every delay will be multiplied by this amount. Default: 1.
    • shouldRetryOnResolve: A function that will be called with the result of the function. If it returns true, the function will be retried. Default: undefined.
    • shouldRetryOnReject: A function that will be called with the error of the function. If it returns true, the function will be retried. Default: undefined.

Tests

You can run the tests by using the following command:

npm test

ToDo

  • [ ] Add event listening (onRetry, etc.)