Skip to content

Latest commit

 

History

History
99 lines (76 loc) · 2.24 KB

README.md

File metadata and controls

99 lines (76 loc) · 2.24 KB

Redo.js

A powerful and lightweight library to help you write robust and fault-tolerant code.

Version


Table of Contents


Installation

Using npm:

npm install redo.js

Using yarn:

yarn add redo.js

Usage

Retry Synchronous Operations

// Import the function
import { retryOperation } from "redo.js";

retryOperation({
  retryCount: 3, // Optional. Default: 3. Number of retry attempts
  retryDelay: 1000, // Optional. Default: 1000ms. Delay in ms between retries
  // incrementalDelayFactor: 2, // Optional. Default: 1.5 Exponential backoff factor
  retryCallback: () => {
    console.log("Retrying operation...");
    throw new Error("Operation failed");
  },
  onErrorCallback: () => {
    console.log("An error occurred.");
  },
  onSuccessCallback: () => {
    console.log("Operation succeeded!");
  },
  afterLastAttemptErrorCallback: (error) => {
    console.error("Final error:", error.message);
  },
});

Retry Asynchronous Operations

import axios from "axios";
import { retryAsyncOperation } from "redo.js";

const fetchData = async () => {
  return await axios({
    url: "https://jsonplaceholder.typicode.com/posts", // Example endpoint
    method: "GET",
  });
};

retryAsyncOperation({
  retryCount: 3, // Optional. Default: 3. Number of retry attempts
  retryDelay: 1000, // Optional. Default: 1000ms. Delay in ms between retries
  // incrementalDelayFactor: 2, // Optional. Default: 1.5 Exponential backoff factor
  retryAsyncCallback: async () => {
    return await fetchData();
  },
  onErrorCallback: (error, currentRetryCount) => {
    console.log(`Retry #${currentRetryCount} failed: ${error.message}`);
  },
  onSuccessCallback: (response) => {
    console.log("Operation succeeded with status:", response.status);
  },
  afterLastAttemptErrorCallback: (error) => {
    console.error("Final error:", error.message);
  },
});

License

Refer to the LICENSE file in the repository.