JSPM

retry-lite

1.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q14573F
    • License MIT

    Minimal async retry utility for Node.js with delay and retry logic

    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-lite

    Using 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