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-call-on-array-items

v1.0.0

Published

wait for all async invocations of functions on all array items

Downloads

1

Readme

async-call-on-array-items-node

"Wait for all async invocations of functions on all array items."

A ridiculously simple package. Probably best not to actually use this, but to learn from it.

The implementation is simply:

module.exports = (func, items) => Promise.all(items.map(func))

What is this for?

If you want to run an async function on all items in an array, you can't just use the normal array methods like forEach() if you want to wait for all the async methods to complete before continuing.

I mostly wrote this to remind myself of this syntax as I find myself using the pattern quite often in async code

Installation

yarn add async-call-on-array-items

or

npm install async-call-on-array-items

Basic usage example

The following code will log out the array: ['hella', 'warld']. For simplicity I am artificially making this potentially synchronous method asynchronous to show the point of the method. The point being that it will wait for all of the called async functions to complete before returning (because of Promise.all)

const asyncCallOnArrayItems = require('async-call-on-array-items')
const pause = delay => new Promise(resolve => { setTimeout(resolve, delay) })

const asyncFunc = async item => {
  await pause(1)
  return item.replace('o', 'a')
}
asyncCallOnArrayItems(asyncFunc, ['hello', 'world']).then(result => console.log(result))

Notice that it doesn't support callbacks and is promisified by default.

API

asyncCallOnArrayItems method

This is the only method available in the library. It is the default export (there are no named exports.)

It expects 2 required parameters.

asyncFunc: function (required)

The async function that you want to run against every item in the array. Note there is no point in using this is your function is not async - just use Array.forEach() in that case.

items: Array (required)

The array containing the items you want passed into each function

Errors

Invalid Arguments

If too few arguments are supplied, then an Error: More arguments needed is thrown. If arguments supplied are of the wrong type, then a TypeError is thrown. If the array is empty, then a RangeError is thrown.

Examples

See basic usage above, not too many variations on that really.

License

ISC (i.e. feel free to use this for any purpose, but I accept no liability for it's use.)

Contributing

Feel free to open issues or even better submit pull requests.

Guidelines for contributing (good pull requests):

  • Please follow the style that is already present in the repository.
  • Please ensure the code passes all lint (eslint) and format (prettier) rules (Check using yarn lint.)
  • Please ensure all (jest) tests are passing (Check using yarn test.)
  • Please ensure all code is covered by tests. (Check the coverage report created by yarn test.)
  • Please ensure any change in the public api is documented properly in the README.

If you would like to become a maintainer, feel free to contact me. You would probably have to have become known to me via submitted pull requests first.

Keywords

  • async
  • arrays