New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@1ib/retry

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@1ib/retry

A TypeScript utility for retrying sync/async methods that throw errors.

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

retry

A TypeScript utility for retrying sync/async methods that throw errors.

Basic usage:

import { retry } from "@1ib/retry"

class {
  // Use the `retry` decorator to retry a method if it throws an error,
  // for a maximum of 3 tries.
  @retry(3)
  foo() {
    // This method can return anything or throw an error.
  }
}

For async methods:

class {
  // Same as before, but an async function, which means it returns a Promise.
  // It also retries up to 3 times if an error is thrown.
  @retry(3)
  async foo() {
    // This method can return anything or throw an error.
  }
}

For custom retry logic:

import fs from "fs/promises"

class {
    // This method retries up to 3 times if an error is thrown,
    // and only if the error is an `ENOENT` or `ENFILE` error.
    @retry(async ({ code }: Result | Error, attempts: number) =>
        (code === "ENFILE" || code === "EMFILE") && attempts < 3)
    async foo(@attempts attemps?: number) {
        console.info(`This method has been called ${attempts} times.`)
        return fs.readFile("README.md", "utf8")
    }
}

Install

Node.js

Install using [npm][npm] or [yarn][yarn]:

npm install @1ib/retry

# or

yarn add @1ib/retry

Import into your Node.js project:

// CommonJS
const { retry } = require("@1ib/retry")

// ESM
import { retry } from "@1ib/retry"

Deno

Install using JSR:

deno add @1ib/retry

#or

jsr add @1ib/retry

Then import into your Deno project:

import { retry } from "@1ib/retry"

Bun

Install using this command:

bun add @1ib/retry

Import into your Bun project:

import { retry } from "@1ib/retry"

License

This project is licensed under the MIT.

Author

kingcc

FAQs

Package last updated on 21 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc