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

delayed-foreach

v1.2.0

Published

A 'delayed' foreach loop that runs at specified intervals

Downloads

1

Readme

delayed-foreach

This package provides synchronous and asynchronous forEach-like functions that loop over an array and execute a specified function on each element, pausing for a specified delay between iterations. The synchronous function uses a blocking loop, while the asynchronous function uses async/await and Promise.

Installation

To install the package, use the following command:

pnpm add delayed-foreach
# or
npm install delayed-foreach
# or
yarn add delayed-foreach

Usage

The package exports two functions: delayedForEachSync and delayedForEach.

import { delayedForEachSync, delayedForEach } from 'delayed-foreach'

// Example array of items
const items = [1, 2, 3, 4, 5]

// Example function to execute on each item
function logItem(item: number) {
  console.log(`Processing item ${item}`)
}

// Example options object
const options = {
  array: items,
  delay: 1000,
  forEach: logItem,
  onCompletion: () => console.log('Completed processing items'),
}

// Use the synchronous function
delayedForEachSync(options)

// Use the asynchronous function
await delayedForEach(options)

Use Cases

Here are a few possible use cases for the delayedForEach function:

Rate Limiting API Requests

If you need to make successive API calls without hitting a rate limit, you can use the delayedForEach function to introduce a delay between each request. For example:

import axios from 'axios'
import { delayedForEach } from 'delayed-foreach'

const userProfileEndpoint = 'https://api.example.com/profiles?user='

// Assume this gets an array of user IDs
const userIDs = await axios.get('https://api.example.com/users')

const users: User[] = []

// Example function to call an API endpoint
async function retrieveUser(userID: string) {
  const response = await axios.get(`${userProfileEndpoint}${userID}`)
  users.push(response.data)
}

// Example options object
const options = {
  array: userIDs,
  delay: 500, // introduce a half-second delay between requests
  forEach: retrieveUser,
  onCompletion: () => {
    console.log('All users have been retrieved.')
    renderUserProfiles(users)
  },
}

// Use the asynchronous function
await delayedForEach(options)

Animating Elements on a Web Page

If you need to animate a set of elements on a web page, you can use the delayedForEach function to introduce a delay between each animation. For example:

import { delayedForEach } from 'delayed-foreach'

// Example array of elements to animate
const elements = document.querySelectorAll('.my-element')

// Example function to animate an element
async function animateElement(element: Element) {
  element.classList.add('animated') // add a CSS class to animate the element
  await new Promise((resolve) => setTimeout(resolve, 1000)) // wait for 1 second
  element.classList.remove('animated') // remove the CSS class to stop the animation
}

// Example options object
const options = {
  array: elements,
  delay: 1000, // introduce a 1-second delay between animations
  forEach: animateElement,
}

// Use the asynchronous function
await delayedForEach(options)

License

MIT