DEV Community

Cover image for Effortless JavaScript Retries with redo.js!
Navdeep Mishra
Navdeep Mishra

Posted on

1

Effortless JavaScript Retries with redo.js!

Effortless JavaScript Retries with redo.js! 🚀

Tired of handling unreliable APIs, flaky database queries, or network failures? Instead of writing complex retry logic, let redo.js handle it for you—simple, powerful, and effortless!

Why redo.js?

✅ Works with Node.js, React, Vue, and more✅ Supports customizable retry strategies (exponential backoff, fixed delays, etc.)✅ Handles errors automatically with minimal code

📦 Installation

  • Using npm:
npm install redo.js
Enter fullscreen mode Exit fullscreen mode
  • Using yarn:
yarn add redo.js
Enter fullscreen mode Exit fullscreen mode

Usage

🔄 Retry Synchronous Operations

import { retryOperation } from "redo.js";

retryOperation({
  retryCount: 3, // Default: 3 - Number of retry attempts
  retryDelay: 1000, // Default: 1000ms - Delay between retries
  // incrementalDelayFactor: 2, // Optional - 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),
});
Enter fullscreen mode Exit fullscreen mode

🔄 Retry Asynchronous Operations

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

const fetchData = async () => {
  return axios.get("https://jsonplaceholder.typicode.com/posts");
};

retryAsyncOperation({
  retryCount: 3,
  retryDelay: 1000,
  // incrementalDelayFactor: 2,

  retryAsyncCallback: async () => 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),
});
Enter fullscreen mode Exit fullscreen mode

🚀 Try redo.js today and make your async workflows resilient!

🔄 Like, share, and support! 😊

Thank you! 🙌

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (2)

Collapse
 
vitalyt profile image
Vitaly Tomilov

Without using any libraries: github.com/vitaly-t/retry-async

Collapse
 
jesperhoy profile image
Jesper Høy

Nice idea :-)

Is the source code available?
Curious about the implementation details.

Does retryAsyncOperation return a Promise so that it can be awaited?
It feels like it should... but then onSuccessCallbackand afterLastAttemptErrorCallback would be superfluous.

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay