als-retry

1.0.0 • Public • Published

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:

  1. action: An asynchronous function to be retried.
  2. 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).

Package Sidebar

Install

npm i als-retry

Weekly Downloads

9

Version

1.0.0

License

MIT

Unpacked Size

6.36 kB

Total Files

4

Last publish

Collaborators

  • alexsorkin