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-looper

v1.0.9

Published

Fast, flexible and simple function that loops through each array or object element to execute asynchronous code.

Downloads

6

Readme

async-looper

NPM version NPM downloads Build Status

Fast, flexible and simple function that loops through each array or object element to execute asynchronous code.

Usage

This function expect four parameters:

  1. Element to loop through
  2. Function to execute each time
  3. Function to execute when loop finish.
  4. Number of times needed to execute nextTick
const loop = require('async-looper')

let counter = 0
let array = [1, 2, 3, 4, 5]

loop(array, (item, next) => {
  // First time
  // item = 1
  counter++
  next()
}, errors => {
  expect(counter).to.be.equals(array.length)
  done()
})

It can loop over objects.

const loop = require('async-looper')

let object = {a: 1, b: 2, c: 3, d: 4}
let newValueToA = 100;

loop(object, (item, next) => {
  // First time
  // item = {
  //  value: 1,
  //  key: 'a'
  // }
  if (item.key === 'a') {
    object[item.key] = newValueToA
  }
  next()
}, errors => {
  expect(object.a).to.be.equal(newValue)
  done()
})

If you need you can stop the loop just passing a second param to the next function.

const loop = require('async-looper')
const END_LOOP = loop.END_LOOP

const limit = 5
let counter = 0
let array = [1, 2, 3, 4, 5, 6]

loop(array, (item, next) => {
  counter++
  if (counter === limit) {
    next(null, END_LOOP)
  } else {
    next()
  }
}, errors => {
  expect(counter).to.be.equal(limit)
  done()
})

The finish callback has an array of errors or it's null

const loop = require('async-looper')

let array = [1, 2, 3, 4, 5, 6]

loop(array, (item, next) => {
  next(new Error())
}, errors => {
  expect(errors).to.have.lengthOf(array.length)
  done()
})

TODO

  1. Improve the documentation.
  2. Benchmarks.
  3. Should we have the key value while loop through an array?