What is p-all?
The p-all package is a utility module for running multiple promise-returning & async functions with limited concurrency, aggregating the results. It is useful for controlling the execution of a large number of asynchronous operations to ensure that system resources are managed properly.
What are p-all's main functionalities?
Running multiple promises with limited concurrency
This feature allows you to run multiple promises with a specified concurrency limit. In the code sample, three tasks are executed with a concurrency limit of 2, meaning only two promises will be running at the same time.
const pAll = require('p-all');
const tasks = [
() => Promise.resolve('Task 1'),
() => Promise.resolve('Task 2'),
() => Promise.resolve('Task 3')
];
pAll(tasks, { concurrency: 2 }).then(results => {
console.log(results); // ['Task 1', 'Task 2', 'Task 3']
});
Other packages similar to p-all
bluebird
Bluebird is a comprehensive promise library with a wide range of features including concurrency control. It offers similar functionality through its 'Promise.map' method with a 'concurrency' option. Bluebird is known for its performance and additional utilities for promise manipulation.
async
The async library provides a collection of asynchronous utilities. Its 'parallelLimit' function can be used to achieve similar concurrency control as p-all. Async is not promise-based by default but supports promises and also provides other flow control utilities.
p-limit
p-limit is a package that limits the number of concurrent promise executions, similar to p-all. The difference is that p-limit provides a lower-level API where you create a limiter function and then apply it to individual promises, whereas p-all takes an array of promise-returning functions.
p-queue
p-queue is a promise queue with adjustable concurrency that can be paused, resumed, and cleared. It is more feature-rich than p-all, providing a queue interface with more control over the execution of tasks, including dynamic prioritization and task cancellation.
p-all
Run promise-returning & async functions concurrently with optional limited concurrency
Similar to Promise.all()
, but accepts functions instead of promises directly so you can limit the concurrency.
If you're doing the same in each call, you should use p-map
instead.
Install
$ npm install --save p-all
Usage
const pAll = require('p-all');
const got = require('got');
const actions = [
() => got('sindresorhus.com'),
() => got('ava.li'),
() => checkSomething(),
() => doSomethingElse()
];
pAll(actions, {concurrency: 2}).then(result => {
console.log(result);
});
API
pAll(input, [options])
Returns a Promise
that is fulfilled when all promises returned from calling the functions in input
are fulfilled, or rejects if any of the promises reject. The fulfilled value is an Array
of the fulfilled values in input
order.
input
Type: Iterable<Function>
Iterable with promise-returning/async functions.
options
Type: Object
concurrency
Type: number
Default: Infinity
Number of concurrent pending promises.
Related
- p-map - Map over promises concurrently
- p-props - Like
Promise.all()
but for Map
and Object
- p-limit - Run multiple promise-returning & async functions with limited concurrency
- More…
License
MIT © Sindre Sorhus