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 🙏

© 2025 – Pkg Stats / Ryan Hefner

repeat-async

v1.0.2

Published

This document explains how to use the exported `repeatAsync` helper provided in this project. `repeatAsync` runs an asynchronous function repeatedly with a fixed delay between executions and provides optional timeout handling, error handling, and a stop m

Readme

repeatAsync

This document explains how to use the exported repeatAsync helper provided in this project. repeatAsync runs an asynchronous function repeatedly with a fixed delay between executions and provides optional timeout handling, error handling, and a stop mechanism.

Contents

  • Basic examples
  • Examples with timeout and error handling
  • Stopping the loop

Parameters

  • delay (number, required): delay in milliseconds between the end of one cycle and the scheduling of the next execution.
  • functionToExec (async function, required): the async function that will be executed each cycle.
  • timeout (number, optional): if provided, each execution is raced against a timeout (in ms). If the function doesn't resolve before the timeout, the timeout handler is invoked.
  • onExcededTimeout (function, optional): called when a single execution exceeds the provided timeout.
  • onError (function, optional): called when functionToExec throws/rejects (or an unexpected error happens). Errors do not stop the loop — the loop continues to the next scheduled cycle.

Return value

  • An object with a single method stop(): calling stop() prevents further executions and clears any pending timer.

Behavior details

  • Each cycle calls functionToExec. If timeout is provided, the implementation races the function against an internal timeout Promise. If the timeout fires first, onExcededTimeout is called. If the function rejects or throws, onError is called.
  • The loop continues indefinitely until stop() is called. Errors do not stop the loop.

Examples

  1. Basic usage (no timeout)
  • A simple example that logs a message every second. Errors are logged via the onError handler but do not stop the loop.
import { repeatAsync } from "repeatAsync";

const runner = repeatAsync({ 
  delay: 1000,

  functionToExec: async () => {
    console.log("executing task");
  },

  onError: (err) => {
    console.error("task error:", err)
  },

});

// Stop after 5 seconds
setTimeout(() => runner.stop(), 5000);
  1. Using a timeout for each execution
  • If the task takes longer than the specified timeout, the onExcededTimeout handler is called. But the loop continues to the next scheduled execution.
import { repeatAsync } from "repeatAsync";

repeatAsync({ delay: 1000, timeout: 1000,
  functionToExec: async () => {
    // Simulate a slow operation
    await new Promise((r) => setTimeout(r, 1500));
  },

  onExcededTimeout: () => {
    console.warn("execution exceeded timeout");
  },

  onError: (err) => {
    console.error("error during execution:", err);
  },
}),
  1. Errors do not stop the loop
  • If the task throws an error, the onError handler is called, but the loop continues executing.
repeatAsync({ delay: 1000,
  functionToExec: async () => {
    throw new Error("boom");
  },

  onError: (err) => {
    console.log("caught error, loop continues:", err.message)
  },
});

Stopping the loop

  • Call the returned stop() method to prevent further executions and clear any pending timers.
const loop = repeatAsync({ delay: 1000, 
    functionToExec: async () => {
        /* ... */
    } 
});

loop.stop();