Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
async-await-parallel
Advanced tools
This module is a simple utility for limiting the concurrency of await
ing 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.
npm install async-await-parallel
This module uses async and await and therefore requires Node >= 7.
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.
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.
/**
* 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)
MIT. Copyright (c) 2017 Vidy.
FAQs
Concurrency control for awaiting an array of async results
The npm package async-await-parallel receives a total of 6,198 weekly downloads. As such, async-await-parallel popularity was classified as popular.
We found that async-await-parallel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.