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

async-delay-planner

v2.0.0

Published

A module for planning delay for a series of function runs

Downloads

3

Readme

Build Status Coverage Status

async-delay-planner

A module for planning delays for a series of function runs

Version 2 now has breaking changes:

  • no speedup as was in version 1;
  • default threshold is Infinity, 0 isn't interpreted as "no threshold";
  • if possible delay is over threshold, function hold promise rejects.

aim

Imagine we have a function which is limited to have minimum intervals between each next run (e.g. API endpoint which can be touched max. 15 times a second, so the min. interval is 1/15 s).

We have an asynchronous function to fetch the API:

async function f() {
  // some API fetching
}

The hypothetical project is complicated and the function has to be run from different places at unpredictable moments.

So:

usage

The aim of the package is to let you add a delay before the function body so it will be executed after a computed timeout.

It is guaranteed that you will not go beyond the time limits between two runs of the function.

installation

run npm i async-delay-planner

require:

const Planner = require('async-delay-planner');

example

1) simple

Let's init the planner:

const planner = new Planner(1000);

We told the planner we have no threshold and the interval is 1000 ms.

And modify the function:

async function f() {
  await planner.hold();
  // some API fetching
}

If we run the function twice like f(); f();, the body of the first function runs immediately and the body of the second runs at least 1000 ms later.

If we have a third run – 2000 ms later and so on.

If the second run doesn't follow the first immediately, but for example after 200 ms, the second body will start after the least of the interval – 800 ms.

hold() just returns a Promise that resolves when time comes, so also feel free to use it without async/await.

2) with threshold

You define a threshold in ms. If the function has to be planned to run later than the threshold, the hold() function fails (the returned promise is rejected).

For example:

const planner = new Planner(1000, 5000);

In this case if we run 10 functions one after another, they will execute:

  • immediately,
  • 1000 ms after the first,
  • 2000 ms after the first,
  • 3000 ms after the first,
  • 4000 ms after the first,
  • immediately fail,
  • immediately fail,
  • immediately fail,
  • immediately fail,
  • immediately fail.

tests

Yes, there are tests for all the cases, you can take a look if you want examples.

Run:

npm test