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

shiterate

v1.1.1

Published

forEach()` loops that wait

Downloads

18

Readme

Shiterate Build Status Dependencies

forEach() loops that wait.

When should I use it?

When you have an array of items that you need to loop through, but for each item in the array you need to perform something asynchronous and you can't continue to the next item in the array until the preceding item has finished. That's when.

Just use promises. Do you even ES6, bro?

Promises will get us close! Except with Promise.all, all promises run in parallel, not one at a time—that's not what we want. But hey, if that's what you'd rather do, here's some starter code:

Promise.all(myCoolArray.map((val, i) => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000);
  });
})).then(() => {
  console.log('done');
});

No, chain the promises with .then()!

I suppose you could…

doSomething(arr[0])
  .then(() => doSomething(arr[1]))
  .then(() => doSomething(arr[2]))
  .then(() => doSomething(arr[3]))
  ...
  .then(() => {
    console.log('done');
  });

{{some awesome package}} already does this!

Why can't you just be happy for me?


Installation

$ npm install shiterate

Quick Start

In the following example, "done" will be logged after three seconds have elapsed.

const shiterate = require('shiterate');

shiterate(['foo', 'bar', 'baz'], (value, n, next) => {
  setTimeout(next, 1000);
}, () => {
  console.log('done');
});

Still confused? Check out a few more examples.


Usage

Definition

shiterate(array, iteratee[, done]);

Arguments

  • array (Array) - The array to query.

  • iteratee (Function) - The function invoked per iteration. It passes the following parameters:

    • value (*) - The value of the array at the current index n during iteration.

    • n (number) - The index of the item that we are currently iterating.

    • next (Function) - The function to call to continue to the next item in array.

      You can change the value of the current item by passing a new value into the next callback.

      next([newValue])

      Calling next.abort() instead will immediately exit the iteratee and invoke the done callback. Similar to next(), you can also pass an argument into next.abort() to change the value of the current item during the abort.

      next.abort([newValue])

  • [done] (Function) - The function invoked when the iteration has finished. It passes the following parameters:

    • values (Array) - The updated slice of array.

Mapping

Shiterate can also be used to act as a map function. By simply passing in a argument to the next() function, you will change the value of the current item. When the iteratee has completed, the updated array will be sent as the first parameter in your done() callback. It should be noted that array is not altered.

In this example, we add 1 to each item in the array.

shiterate([0, 1, 2], (value, n, next) => {
  setTimeout(() => {
    return next(value + 1);
  }, 1000);
}, values => {
  console.log(values);
  // => [1, 2, 3]
});

Why "Shiterate"?

Obviously a vulgar fusion of shit and iterate, it appealed to me because:

  1. It wasn't taken on npm :tada:

  2. It's concise; "iterate" is already in the name.

  3. Although this concept does have a few practical applications, in many cases it's unnecessary and impedes the flow of the program. This might be considered "ugly" or "shitty" when done without purpose.

  4. I don't give a shit


Examples

  1. Continue to the next item in the array after the number of seconds elapsed is equal to the index of the current item. We also append "-qux" to the item value. This example will take three seconds to finish.

    shiterate(['foo', 'bar', 'baz'], (value, n, next) => {
      setTimeout(() => {
        return next(value + '-qux');
      }, 1000 * n);
    }, values => {
      console.log(values);
      // => ["foo-qux", "bar-qux", "baz-qux"]
    });
  2. Square the value of the current item, but abort the iterator if the new value is greater than 25.

    shiterate([4, 3, 5, 7, 6], (value, n, next) => {
      setTimeout(() => {
        if (value * value <= 25) {
          return next();
        } else {
          return next.abort();
        }
      }, 1000);
    });

Authors

License

MIT