Package Exports
- retry-lite
- retry-lite/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (retry-lite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
๐ retry-lite
A minimal async retry utility for Node.js.
Easily retry failing async functions with custom delay, retry count, and retry condition. Useful for API requests, database queries, file reads, and any flaky operations.
๐ฆ Installation
Using npm
npm install retry-liteUsing yarn
yarn add retry-lite๐ Usage
Basic usage with default settings
const retry = require("retry-lite");
let attempt = 0;
async function unstable() {
attempt++;
if (attempt < 3) throw new Error(`Failed attempt ${attempt}`);
return "Success!";
}
(async () => {
const result = await retry(unstable);
console.log(result); // "Success!"
})();Customize retries and delay
await retry(unstable, {
retries: 5, // default: 3
delay: 1000, // default: 1000ms
});Use conditional retry logic
await retry(() => fetchData(), {
retries: 4,
delay: 500,
shouldRetry: (error, attempt) => {
return error.message.includes("rate limit");
}
});โ๏ธ Options
| Option | Type | Default | Description |
|---|---|---|---|
retries |
number |
3 |
Maximum number of retry attempts |
delay |
number |
1000 |
Delay between retries (in milliseconds) |
shouldRetry |
function |
Always | A function (error, attempt) => boolean to conditionally retry |
๐ค Output Example
Fail #1
Fail #2
Success!If all attempts fail, the final error will be thrown.
๐งช Run Test
node test.js๐ชช License
MIT