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-await-parallel

v1.0.0

Published

Concurrency control for awaiting an array of async results

Downloads

36,676

Readme

async-await-parallel travis npm

This module is a simple utility for limiting the concurrency of awaiting async arrays in ES7. It replaces Promise.all when using async await much like async.mapLimit is commonly used in place of the analogous async.map when using callback-style async functions.

Installation

npm install async-await-parallel

This module uses async and await and therefore requires Node >= 7.

Background

Normally, when you have an array of async operations that you want to await on, you would use Promise.all.

await Promise.all([
  async () => { ... },
  async () => { ... },
  async () => { ... },
  async () => { ... },
  async () => { ... },
])

Unfortunately, there's nothing built into ES7's implementation of async await that allows you to limit the concurrency of how many async handlers are running at once.

This is problematic in many common scenarios such as performing operations on each file in a directory or downloading a batch of URLs without opening too many sockets or clogging IO bandwidth.

Usage

async-await-parallel allows you to set a maximum concurrency for an array of async results you want to await on.

const parallel = require('async-await-parallel')

await parallel([
  async () => { ... },
  async () => { ... },
  async () => { ... },
  async () => { ... },
  async () => { ... },
], 2)

In this example, a max concurrency of 2 is set, so no more than 2 of the async functions may execute concurrently. Async functions will be executed in order once previous ones resolve.

API

/**
 * Invokes an array of async functions in parallel with a limit to the maximum
 * number of concurrent invocations. Async functions are executed in-order and
 * the results are mapped to the return array.
 *
 * Acts as a replacement for `await Promise.all([ ... ])` by limiting the max
 * concurrency of the array of function invocations.
 *
 * If any single task fails (eg, returns a rejected Promise), the pool will drain
 * any remaining active tasks and reject the resulting Promsie.
 *
 * @param {Array<async Function(Void) => Any>} thunks
 * @param {Number?} concurrency Max concurrency (defaults to 5)
 *
 * @return {Promise<Array<Any>>}
 */
async function parallel (thunks, concurrency = 5)

Inspiration

  • async.mapLimit equivalent functionality for callbacks
  • co-parallel equivalent functionality for co generators
  • async-parallel this library is heavily inspired by this TypeScript library for async / await by Dave Templin (simplified, converted from TS to ES, and added tests)

License

MIT. Copyright (c) 2017 Vidy.