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

@codeo/eventually

v1.2.0

Published

Retry async operations with back-off, handle errors by halting, suppressing or failing

Downloads

194

Readme

eventually

Retry async operations with back-off, handle errors by halting, suppressing or failing.

Examples

  1. Easily retry an async function up to 3 times, throwing if the last attempt fails

    import { eventually } from "@codeo/eventually";
    const result = await eventually(() => queryApi(1, 2, 3), { retries: 3 });
  2. Retry an async function with a back-off strategy: the passed array is a set of millisecond values to back off by. Once the last one is reached, that value is used for any further delays to retry attempts. If the following code were to fail all 5 times, it would back off by 50ms on the second attempt, 250ms on the third attempts, 1 second on the fourth and fifth attempts.

    import { eventually } from "@codeo/eventually";
    const result = await eventually(() => queryApi("user input"), {
        retries: 5,
        backoff: [ 50, 250, 1000 ]
    });

    backoff may also be specified as a single numeric value to use for all attempts.

  3. Halt on error: the promise never resolves. Useful for, eg, polling code where it's ok for an occasional failure or where failures are simply non-fatal, such as polling for an unread message count, which can fail when your app is backgrounded.

    import { eventually, ErrorHandlingStrategies } from "@codeo/eventually";
    const result = await eventually(() => poll("message-count"), {
       retries: 2,
       fail: async(e) => {
           return ErrorHandlingStrategies.halt;
       }
    });
    // if the above fails completely, then this part of code is never reached
    console.log(`you have ${result} messages`);
  4. Suppress an error: in this case, the caller continues with execution (will not halt the promise chain or cause an await to "deadlock") and undefined is returned to the caller. Since fail is an async function, you may choose how to proceed based on the error you've received. Possible strategies are:

    • fail -> pass on the failure
    • halt -> stop the promise chain right here
    • suppress -> completes the chain, but resolves undefined
    • retry -> just try again
    import { eventually, ErrorHandlingStrategies } from "@codeo/eventually";
    const result = await eventually(() => willFail(), {
        retries: 1,
        fail: async(e: Error) => {
            return ErrorHandlingStrategies.suppress;
        }
    });
  5. Your fail handler may even return a new IEventuallyOptions object to completely change the flow of logic

  6. In addition, you may redirect errors with the redirect property on the provided options, for instance when you have a valid value to fall back on when your async function fails:

    import { eventually } from "@codeo/eventually";
    const fallbackValue = 1;
    const result = await eventually(pollServer, {
       redirect: (e: Error) => fallbackValue
    });

    which you could use to query a secondary api eg, when the first is too loaded:

    import { eventually } from "@codeo/eventually";
    const fallbackValue = 1;
    const result = await eventually(pollServer1, {
       redirect: (e: Error) => eventually(pollServer2, {
           redirect: (e: Error) => fallbackValue
       })
    });