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

@humanwhocodes/retry

v0.4.1

Published

A utility to retry failed async methods.

Downloads

43,280,644

Readme

Retry utility

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation or nominate me for a GitHub Star.

Description

A utility for retrying failed async JavaScript calls based on the error returned.

Usage

Node.js

Install using npm or yarn:

npm install @humanwhocodes/retry

# or

yarn add @humanwhocodes/retry

Import into your Node.js project:

// CommonJS
const { Retrier } = require("@humanwhocodes/retry");

// ESM
import { Retrier } from "@humanwhocodes/retry";

Deno

Install using JSR:

deno add @humanwhocodes/retry

#or

jsr add @humanwhocodes/retry

Then import into your Deno project:

import { Retrier } from "@humanwhocodes/retry";

Bun

Install using this command:

bun add @humanwhocodes/retry

Import into your Bun project:

import { Retrier } from "@humanwhocodes/retry";

Browser

It's recommended to import the minified version to save bandwidth:

import { Retrier } from "https://cdn.skypack.dev/@humanwhocodes/retry?min";

However, you can also import the unminified version for debugging purposes:

import { Retrier } from "https://cdn.skypack.dev/@humanwhocodes/retry";

API

After importing, create a new instance of Retrier and specify the function to run on the error. This function should return true if you want the call retried and false if not.

// this instance will retry if the specific error code is found
const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

Then, call the retry() method around the function you'd like to retry, such as:

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));

The retry() method will either pass through the result on success or wait and retry on failure. Any error that isn't caught by the retrier is automatically rejected so the end result is a transparent passing through of both success and failure.

Setting a Timeout

You can control how long a task will attempt to retry before giving up by passing the timeout option to the Retrier constructor. By default, the timeout is one minute.

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
}, { timeout: 100_000 });

const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));

When a call times out, it rejects the first error that was received from calling the function.

Setting a Concurrency Limit

When processing a large number of function calls, you can limit the number of concurrent function calls by passing the concurrency option to the Retrier constructor. By default, concurrency is 1000.

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
}, { concurrency: 100 });

const filenames = getFilenames();
const contents = await Promise.all(
    filenames.map(filename => retrier.retry(() => fs.readFile(filename, "utf8"))
);

Aborting with AbortSignal

You can also pass an AbortSignal to cancel a retry:

import fs from "fs/promises";

const controller = new AbortController();
const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

const text = await retrier.retry(
    () => fs.readFile("README.md", "utf8"),
    { signal: controller.signal }
);

Developer Setup

  1. Fork the repository
  2. Clone your fork
  3. Run npm install to setup dependencies
  4. Run npm test to run tests

Debug Output

Enable debugging output by setting the DEBUG environment variable to "@hwc/retry" before running.

License

Apache 2.0

Prior Art

This utility is inspired by, and contains code from graceful-fs.