async-retry vs promise-poller vs promise-retry vs retry vs wait-for-expect
JavaScript Retry Mechanisms
async-retrypromise-pollerpromise-retryretrywait-for-expectSimilar Packages:

JavaScript Retry Mechanisms

These npm packages provide various strategies for retrying asynchronous operations in JavaScript, particularly useful in scenarios where operations may fail due to transient errors, such as network requests or database queries. They help ensure that applications can gracefully handle failures and improve reliability by automatically retrying failed operations based on defined rules and conditions.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
async-retry01,915-305 years agoMIT
promise-poller0119-87 years agoMIT
promise-retry0318-116 years agoMIT
retry01,259-205 years agoMIT
wait-for-expect029838.3 kB109 months agoMIT

Feature Comparison: async-retry vs promise-poller vs promise-retry vs retry vs wait-for-expect

Retry Logic

  • async-retry:

    async-retry offers a flexible retry logic that allows you to specify the number of attempts, delay between retries, and custom error handling. It supports exponential backoff strategies, making it suitable for handling transient errors in network requests or database operations.

  • promise-poller:

    promise-poller implements a polling mechanism that repeatedly invokes a promise-returning function until a specified condition is met. It allows you to define the polling interval and timeout, making it ideal for scenarios where you need to wait for a resource to become available.

  • promise-retry:

    promise-retry provides a simple way to retry promise-returning functions with customizable retry logic. You can specify the number of retries, delay between attempts, and handle specific error cases, making it effective for handling failures in asynchronous operations.

  • retry:

    retry is a versatile package that can handle both synchronous and asynchronous functions. It allows you to define retry strategies, including maximum attempts and delays, and provides a straightforward API for implementing retries across various use cases.

  • wait-for-expect:

    wait-for-expect is tailored for testing scenarios, allowing you to wait for specific expectations to be met in asynchronous tests. It simplifies the process of writing tests that involve asynchronous operations, ensuring that your assertions are only checked when the expected conditions are satisfied.

Use Cases

  • async-retry:

    Best suited for applications that make frequent network requests or interact with unreliable services, where transient failures are common and need to be handled gracefully with retries.

  • promise-poller:

    Ideal for scenarios where you need to check the status of an operation or resource repeatedly until it becomes available, such as waiting for a file to be uploaded or a service to be ready.

  • promise-retry:

    Great for retrying operations that may fail due to temporary issues, such as API calls, where you want to implement a simple retry mechanism without complex configurations.

  • retry:

    Useful for a wide range of applications that require retries for both synchronous and asynchronous operations, including database queries and external service calls.

  • wait-for-expect:

    Specifically designed for testing frameworks, making it essential for writing tests that involve asynchronous operations and ensuring that your tests are reliable and accurate.

Configuration Flexibility

  • async-retry:

    Highly configurable, allowing developers to customize retry attempts, delays, and error handling strategies to fit specific application needs. It supports both linear and exponential backoff strategies.

  • promise-poller:

    Provides options for customizing polling intervals and timeouts, giving developers control over how often to check for conditions and how long to wait before giving up.

  • promise-retry:

    Offers straightforward configuration options for retries, including the number of attempts and delay, making it easy to implement without extensive setup.

  • retry:

    Flexible in defining retry strategies, allowing for detailed configurations that can adapt to various use cases, whether for synchronous or asynchronous functions.

  • wait-for-expect:

    Simplifies configuration for test scenarios, allowing developers to specify the conditions and timeouts for expectations in a clear and concise manner.

Error Handling

  • async-retry:

    Includes built-in support for handling specific errors, allowing developers to define which errors should trigger a retry and which should not, enhancing control over the retry process.

  • promise-poller:

    Handles errors gracefully by allowing the polling to continue until the condition is met or the timeout is reached, ensuring that transient errors do not halt the polling process.

  • promise-retry:

    Provides mechanisms to handle errors during retries, allowing developers to specify which errors should trigger a retry and how to manage them effectively.

  • retry:

    Offers robust error handling capabilities, enabling developers to specify retry conditions based on error types, making it suitable for complex error scenarios.

  • wait-for-expect:

    Focuses on ensuring that expectations are met before proceeding, providing clear error messages when expectations fail, which is crucial for debugging tests.

Learning Curve

  • async-retry:

    Relatively easy to learn, especially for developers familiar with promises and async/await syntax. Its API is straightforward and intuitive, making it accessible for quick implementation.

  • promise-poller:

    Simple to understand and use, especially for those familiar with polling concepts. The API is designed to be user-friendly, allowing for quick integration into projects.

  • promise-retry:

    Very easy to grasp, with a minimalistic API that allows developers to implement retries with just a few lines of code, making it suitable for quick solutions.

  • retry:

    Moderate learning curve due to its versatility and broader use cases. However, once understood, it can be applied to various scenarios effectively.

  • wait-for-expect:

    Designed for developers familiar with testing frameworks, it has a straightforward API that integrates seamlessly into test cases, making it easy to adopt.

How to Choose: async-retry vs promise-poller vs promise-retry vs retry vs wait-for-expect

  • async-retry:

    Choose async-retry if you need a simple and flexible way to retry asynchronous functions with customizable retry strategies, including delays and maximum attempts. It is particularly useful for handling promises and async/await syntax.

  • promise-poller:

    Select promise-poller when you need to repeatedly execute a promise-returning function until a certain condition is met, providing a polling mechanism that can be customized with intervals and timeouts. It's ideal for scenarios where you want to wait for a resource to become available.

  • promise-retry:

    Use promise-retry if you want a straightforward way to retry promise-returning functions with a focus on handling errors and customizing the retry logic. It allows for a simple configuration of retries and delays, making it suitable for straightforward retry needs.

  • retry:

    Opt for retry when you want a more general-purpose retry mechanism that can handle both synchronous and asynchronous functions. It provides a robust API for defining retry strategies and is useful for retrying operations that may not return promises.

  • wait-for-expect:

    Choose wait-for-expect if you are writing tests and need a way to wait for asynchronous expectations to be met. This package is specifically designed for testing frameworks and helps ensure that your tests only proceed once certain conditions are satisfied.

README for async-retry

async-retry

Retrying made simple, easy, and async.

Usage

// Packages
const retry = require('async-retry');
const fetch = require('node-fetch');

await retry(
  async (bail) => {
    // if anything throws, we retry
    const res = await fetch('https://google.com');

    if (403 === res.status) {
      // don't retry upon 403
      bail(new Error('Unauthorized'));
      return;
    }

    const data = await res.text();
    return data.substr(0, 500);
  },
  {
    retries: 5,
  }
);

API

retry(retrier : Function, opts : Object) => Promise
  • The supplied function can be async or not. In other words, it can be a function that returns a Promise or a value.
  • The supplied function receives two parameters
    1. A Function you can invoke to abort the retrying (bail)
    2. A Number identifying the attempt. The absolute first attempt (before any retries) is 1.
  • The opts are passed to node-retry. Read its docs
    • retries: The maximum amount of times to retry the operation. Default is 10.
    • factor: The exponential factor to use. Default is 2.
    • minTimeout: The number of milliseconds before starting the first retry. Default is 1000.
    • maxTimeout: The maximum number of milliseconds between two retries. Default is Infinity.
    • randomize: Randomizes the timeouts by multiplying with a factor between 1 to 2. Default is true.
    • onRetry: an optional Function that is invoked after a new retry is performed. It's passed the Error that triggered it as a parameter.

Authors