Skip to content

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

License

Notifications You must be signed in to change notification settings

navdeepm20/redo.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published