retry
A TypeScript utility for retrying sync/async methods that throw errors.
Basic usage:
import { retry } from "@1ib/retry"
class {
@retry(3)
foo() {
}
}
For async
methods:
class {
@retry(3)
async foo() {
}
}
For custom retry logic:
import fs from "fs/promises"
class {
@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:
const { retry } = require("@1ib/retry")
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