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

@mcastiello/time-functions

v0.0.4

Published

A collection of functions that can be used to simplify the asynchronous execution of your code.

Downloads

17

Readme

Time Functions

A collection of functions that can be used to simplify the asynchronous execution of your code.

Install

yarn add @mcastiello/time-functions

npm install @mcastiello/time-functions

Functions

debounce

Delay the execution of a function. If while waiting for the execution, the function is called again, the previous call is cancelled and delay is reset. The function will not be executed until the delay is successfully completed without any rest.

import { debounce, sleep } from "@mcastiello/time-functions";

const myFunction = () => console.log("Hello World!");

const debounced = debounce(myFunction, 20);

debounced();
debounced();
debounced();

await sleep(50);
debounced();

// Hello World!
// Hello World!

throttle

Run the function the first time is called, but hold successive executions until the requested delay is reached.

import { throttle, sleep } from "@mcastiello/time-functions";

const myFunction = () => console.log("Hello World!");

const throttled = throttle(myFunction, 20);

throttled();
// Hello World!

throttled();
throttled();

await sleep(50);
throttled();
// Hello World!

sleep

Put on hold an asynchronous execution for the requested delay.

import { sleep } from "@mcastiello/time-functions";

sleep(50).then(() => {
  console.log("Hello World!");
})

retry

Keep retrying to execute the provided function until it returns a valid value without throwing errors, or until the timeout is reached.

import { retry } from "@mcastiello/time-functions";
import { API_ENDPOINT } from "./constants";

const makeRequest = retry(async (url: string): Promise<{ valid: boolean }> => {
  const response = await fetch(url);
  return await response.json();
}, {
  delay: 250,
  timeout: 5000,
  validate: (result) => result.valid
});

try {
  console.log(await makeRequest(API_ENDPOINT));
} catch (error) {
  console.error(error);
}