1. @humanwhocodes/retry
A utility to retry failed async methods.
@humanwhocodes/retry
Package: @humanwhocodes/retry
Created by: humanwhocodes
Last modified: Fri, 03 May 2024 14:49:20 GMT
Version: 0.2.4
License: Apache-2.0
Downloads: 3,403
Repository: https://github.com/humanwhocodes/retrier

Install

npm install @humanwhocodes/retry
yarn add @humanwhocodes/retry

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.

Developer Setup

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

License

Apache 2.0

Prior Art

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

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin