als-retry
v1.0.0
Published
A lightweight library for retrying tasks with configurable attempts and intervals.
Downloads
8
Maintainers
Readme
als-retry
als-retry
is a lightweight library designed for retrying tasks with a specified number of attempts and interval between retries. It's useful when handling tasks that can temporarily fail and require retries.
Can be used in browser too.
Installation
To install the library, run the following command:
npm install als-retry
Usage
Simple Example
const retry = require("als-retry");
async function fetchData() {
// Example task that might fail temporarily
const success = Math.random() > 0.5;
if (!success) throw new Error("Temporary failure in fetching data");
return "Data received!";
}
async function main() {
try {
const result = await retry(fetchData, { attempts: 3, waitTime: 1000, timeout:5000 });
console.log(result); // { result: 'Data received!', errors: [], attempt: 1 }
} catch (error) {
console.error("All attempts failed:", error);
}
}
main();
Settings
The retry
function accepts two parameters:
action
: An asynchronous function to be retried.options
: An object with configuration parameters, including:attempts
(number): Number of attempts (default:3
).waitTime
(number): Interval between attempts in milliseconds (default:1000
).timeout
(number): Timeout for all attempts in milliseconds (default:5000
).