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

recaller

v2.0.0

Published

Promise-based function retry utility.

Downloads

19

Readme

recaller

npm version CI codecov

Promise-based function retry utility. Designed for async/await.

npm install recaller


usage

example partially stolen from async-retry's example

import recaller, { constantBackoff } from "recaller";
import fetch from "node-fetch";

export default async function fetchSomething() {
  return await recaller(
    async (bail, attempt) => {
      const res = await fetch("https://google.com");

      if (403 === res.status) {
        // we're not going to retry
        return bail(new Error("Unauthorized"));
      }

      const data = await res.text();
      return data.substr(0, 500);
    },
    {
      // default: 2 retries
      retries: 10,
      // default: no backoff, retry immediately
      backoff: constantBackoff(1000),
    },
  );
}

api

The code is fully TSDoc'd. See src/index.ts for documentation of the main functions, listed below:

  • recaller(fn, opts)
  • constantBackoff(ms)
  • fullJitterBackoff(opts)

backoffs

recaller doesn't backoff (wait before retrying) by default. To specify backoff, you must give it a backoff function in the options (opts.backoff).

example:

import recaller, { constantBackoff } from 'recaller'

export default function doSomething () {
  return await recaller(async () => {
    const res = await fetch('https://google.com')
  }, {
    // on every failure, wait 5 seconds before retrying
    backoff: constantBackoff(5000)
  })
}

A backoff function, given an attempt count, returns the next delay to wait in milliseconds. For example, constantBackoff(ms) below:

function constantBackoff(ms) {
  ms = ms ?? 5000;
  return (attempt) => ms;
}

recaller comes with 5 backoff generator functions, inspired by AWS's exponential backoff blog post.

Use fullJitterBackoff for most cases, as it generally gives you the best results. You only really have to tweak the base and cap with it. See code for more documentation.

  • constantBackoff(ms)
  • fullJitterBackoff({base, cap, factor})

the following aren't recommended, and only exist for completeness:

  • exponentialBackoff({base, cap, factor})
  • equalJitterBackoff({base, cap, factor})
  • decorrelatedJitterBackoff({base, cap, times})

handling retries

You can intercept each retry attempt, by providing a function in opts.onretry.

import recaller from 'recaller'

export default function doSomething () {
  return await recaller(async () => {
    const res = await fetch('https://google.com')
  }, {
    onretry: function (err, attempt, delayTime) {
      // Prevent retries; reject the recaller with the last error
      if (err instanceof TypeError) throw err

      // err is the error of the attempt
      // attempt is the attempt #. If the first call failed, then attempt = 1.
      // delayTime is how long we will wait before next attempt.

      logger.warn(`doSomething attempt ${attempt} failed;
        will wait ${delayTime} ms before trying again.
        error: ${err}
      `)
    }
  })
}