npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

retry-promise-handler

v0.2.0

Published

A package for retrying promises with various strategies.

Downloads

9

Readme

retry-promise-handler

About

retry-promise-handler is a package designed to facilitate retrying promises with various backoff strategies. It provides flexibility in configuring the number of retries, backoff strategies, and callbacks for success and failure.

Getting Started

Install with npm: npm install retry-promise-handler

Then you can import it with:

import { RetryPromiseHandler } from "retry-promise-handler";

const promise = () => Promise.resolve("Promise fulfilled");
const pHandler = new RetryPromiseHandler(promise, {
  retries: 3,
  backOff: "CUSTOM",
  backOffAmount: [500, 500, 1000],
});

To initiate the retry process, simply call pHandler.start() after configuring the RetryPromiseHandler.

pHandler.start();

Depending on your use case, you can manually stop the retry process if needed. If you choose to stop the retry process manually, you can call

pHandler.stop();

Configuration Options

retry-promise-handler offers two types of configurations: default and custom.

Default configuration

The default configuration provides a simpler approach to retrying promises. It allows you to specify the number of retries, the backoff strategy, and optional callback functions for different events.

export type DefaultBackOffConfiguration<T, R extends number> = {
  retries?: R | "INFINITE";
  backOff?: "FIXED" | "LINEAR" | "EXPONENTIAL";
  backOffAmount?: number;
  onSuccess?: (result: T) => void;
  onFailedRetry?: (error: RetryError) => void;
  onFailedRetryProcess?: (error: FinalError) => void;
  shouldRetryOnCondition?: (error: RetryError) => boolean;
};
  • retries: specifies the number of retries or "INFINITE" for unlimited retries.
  • backOff: defines the backoff strategy, which can be "FIXED", "LINEAR", or "EXPONENTIAL".
  • backOffAmount: Specifies the amount of time to wait between retries.
  • onSuccess: optional callback function executed upon successful promise fulfillment.
  • onFailedRetry: optional callback function executed when a retry fails.
  • onFailedRetryProcess: optional callback function executed when all retries fail.
  • shouldRetryOnCondition: optional function that determines whether to retry. It should return a boolean value, indicating whether or not to retry, based on any relevant condition.

Custom configuration

The custom configuration allows for more fine-grained control over retry behavior. It specifies the exact number of retries and requires an array of custom backoff times, where each element in the array represents the amount of time to wait before each retry.

export type CustomBackOffConfiguration<T, R extends number> = {
  retries: R;
  backOff: "CUSTOM";
  backOffAmount: ArrayOfLength<R, number>;
  onSuccess?: (result: T) => void;
  onFailedRetry?: (error: RetryError) => void;
  onFailedRetryProcess?: (error: FinalError) => void;
  shouldRetryOnCondition?: (error: RetryError) => boolean;
};
  • retries: specifies the exact number of retries.
  • backOff: indicates the use of a custom backoff strategy, which is set to "CUSTOM".
  • backOffAmount: Requires an array of length R containing custom backoff times. Each element in the array represents the amount of time to wait before retrying.
  • onSuccess, onFailedRetry, onFailedRetryProcess, shouldRetryOnCondition: Optional callbacks, same as in the default configuration.

Error handling helpers

The package offers utility functions to identify custom errors, facilitating the distinction of error scenarios when the retry process fails:

  • isErrorAllRetriesFailedError
  • isErrorExitConditionMetError
  • isErrorRetryManuallyStoppedError

Contributing

Whether you want to report a bug, request a feature or submit a pull request, your contribution is greatly appreciated.

Don't forget to show your support by giving the project a star!

License

Distributed under the MIT License. See LICENSE for more information.