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

@avocajs/echo

v1.0.0

Published

A library for retrying both sync and async jobs with customizable retry attempts and delays.

Downloads

1

Readme

@AvocaJs/Echo

The Echo class is a utility that provides robust functionality for retrying asynchronous and synchronous jobs with configurable delay settings. It also includes methods for sleeping and setting/getting various configuration options.

Table of Contents

Installation

To install the Echo utility, use the following command:

npm install @avocajs/echo

Usage

Instantiating Echo

You can create an instance of the Echo class with optional parameters to set the maximum number of retries, the delay between retries, and any additional delay after each retry.

import { Echo } from "@avocajs/echo";

const echo = new Echo(3, 500, 0); // Default values
// maxRetry: 3, retryDelay: 500ms, extraDelay: 0ms

Retrying Jobs

The retry method allows you to retry a given job (function) a specified number of times until it either resolves (for async jobs) or returns a value (for sync jobs), or the retries are exhausted. Here's how it works:

  • Resolves: The retry method resolves when the job resolves or returns a value. This means the job retry is successful, and the retry ends.
  • Rejects: The retry method rejects when the job continues to throw an error (sync) or reject (async) after the specified number of retries.

Example with Asynchronous Job

const echo = new Echo();

const asyncJob = async () => {
  // Simulate a job that may fail
  if (Math.random() > 0.5) {
    throw new Error("Random failure");
  }
  return "Success!";
};

echo
  .retry(asyncJob)
  .then((result) => console.log(result)) // Logs "Success!" if the job succeeds within the retry limit
  .catch((error) => console.error(error)); // Logs the error if all retries fail

Example with Synchronous Job

const echo = new Echo();

const syncJob = () => {
  // Simulate a job that may fail
  if (Math.random() > 0.5) {
    throw new Error("Random failure");
  }
  return "Success!";
};

echo
  .retry(syncJob)
  .then((result) => console.log(result)) // Logs "Success!" if the job succeeds within the retry limit
  .catch((error) => console.error(error)); // Logs the error if all retries fail

Setting Configuration

You can configure the retry behavior using the provided setter methods.

const echo = new Echo();

// Set maximum number of retries
echo.setMaxRetry(5);

// Set delay between retries
echo.setRetryDelay(1000);

// Set additional delay after each retry
echo.setExtraDelay(200);

Sleeping

The sleep method allows you to pause execution for a specified number of milliseconds.

await Echo.sleep(1000); // Sleeps for 1 second

API

Echo Class

Constructor

new Echo(maxRetry?: number, retryDelay?: number, extraDelay?: number);
  • maxRetry (optional): The maximum number of retries (default: 3)
  • retryDelay (optional): The delay between retries in milliseconds (default: 500)
  • extraDelay (optional): The additional delay after each retry in milliseconds (default: 0)

Methods

retry<T>(job: Job<T>): Promise<T>

Retries the given job the specified number of times. Resolves when the job resolves or returns a value. Rejects when the job continues to fail after the specified number of retries.

static sleep(retryDelay: number): Promise<void>

Delays execution for the specified number of milliseconds.

setMaxRetry(maxRetry: number): void

Sets the maximum number of retries.

setRetryDelay(retryDelay: number): void

Sets the delay between retries.

setExtraDelay(extraDelay: number): void

Sets the additional delay after each retry.

getMaxRetry(): number

Gets the maximum number of retries.

getRetryDelay(): number

Gets the delay between retries.

getExtraDelay(): number

Gets the additional delay after each retry.

Error Handling

The Echo class uses a custom error type EchoError to indicate errors related to invalid configuration options.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Author

Essefri Mohamed For any queries, you can reach me at [email protected].