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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@viveknayyar/re-promise

v0.0.3

Published

Retry failed api calls with a backoff time

Downloads

8

Readme

All Contributors

Build Status

🛠 Installation

yarn add @viveknayyar/re-promise

or

npm i @viveknayyar/re-promise --save

🧠 Usage


// some js function in your code which fetches users data

const fetchUser = id => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({ id: 'vivek', name: 'vivek' });
    }, 10);
  });
};
// with es6 await
import { retryPromise } from '@viveknayyar/re-promise';

async function retryAndFetchUser(id) {
  try {
    // await on this promise to resolve if you are using es6 await
    const resp = await retryPromise({
      fn: () => fetchUser(id),
      retries: 3,
      retryDelay: 2000,
      retryOn: function(e) {
        return [500, 502].includes(e.response.status);
      }
    });
  } catch (e) {
    console.log(e);
    return Promise.reject(e);
  }
}
// with traditional promises
import { retryPromise } from '@viveknayyar/re-promise';

function retryAndFetchUsers(id) {
  return retryPromise({
    fn: () => fetchUser(id),
    retries: 3,
    retryDelay: 2000,
    retryOn: function(e) {
      return [500, 502].includes(e.response.status);
    }
  }).then(resp => {
    return resp;
  }).catch(e => {
    console.log(e);
    return Promise.reject(e);
  });
}

🧩 Api

| Props | Type | Default | Description | |-------------------------|------------------------|-----------|-----------------------------------------------------------------------------------------------------| | fn | function | null | function to call which should return a promise | | retries | number | 3 | The maximum amount of times to retry the operation. | | retryDelay | number | 3000 | The number of milliseconds before starting the retry. | | retryOn | function | null | function to specify a custom logic to decide if retry should be done or not | | backOffFactor | function | 1 | The exponential factor to use. Default is 1 | | debug | boolean | false | print information around retry attempts |

fn (required)

This function will be called every time we want to retry a promise or an api call. Make sure you return a promise from this function. It has the following signature:

function fn() {
  return axios.get('some url');
}

retryOn (not compulsory)

This function will be called every time we have to decide if the promise should be retried or not. This function will be called with the error object so you can put in logic inside this function to return true or false based on which we will decide if the api call should be retried or not.

function retryOn(error) {
  // logic to retry on certain status codes
  return [502,500].includes(error.response.status);
}

backOffFactor (not compulsory)

This param will decide the exponential delay between retry attempts. For example:

// with es6 await and backOffFactor
import { retryPromise } from '@viveknayyar/re-promise';

async function retryAndFetchUser(id) {
  try {
    // await on this promise to resolve if you are using es6 await
    const resp = await retryPromise({
      fn: () => fetchUser(id),
      retries: 3,
      retryDelay: 2000,
      backOffFactor: 3,
      retryOn: function(e) {
        return [500, 502].includes(e.response.status);
      }
    });
  } catch (e) {
    console.log(e);
    return Promise.reject(e);
  }
}

In the above case, our first retry call will happen after the retryDelay of 2000ms. The next retry call will now happen after retryDelay*backOffFactor which is 6000ms and similarly we will continue for the next call.

🧳 Size

🎈 Contribute

Show your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !

📝 License

MIT © viveknayyar

👷 TODO

  • [x] Complete README
  • [ ] Add Examples and Demo
  • [x] Test Suite

🎊 Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!