Promise Utility Libraries Comparison
p-all vs p-waterfall vs p-series vs p-props
1 Year
p-allp-waterfallp-seriesp-propsSimilar Packages:
What's Promise Utility Libraries?

Promise utility libraries provide developers with a set of tools to manage and manipulate promises in a more efficient and organized manner. These libraries help streamline asynchronous operations, allowing for better control over the execution flow of multiple promises. Each package offers unique features that cater to different use cases, enabling developers to choose the right tool based on their specific needs for handling concurrency, sequencing, and data management in asynchronous programming.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
p-all1,487,2803205.42 kB12 years agoMIT
p-waterfall1,167,78474-14 years agoMIT
p-series32,44566-03 years agoMIT
p-props23,1261946.58 kB1a year agoMIT
Feature Comparison: p-all vs p-waterfall vs p-series vs p-props

Execution Model

  • p-all:

    p-all executes all promises concurrently, returning a single promise that resolves when all input promises have resolved. It does not care about the order of completion, making it suitable for independent tasks that can run in parallel.

  • p-waterfall:

    p-waterfall runs a series of functions in sequence, passing the result of each function to the next. This creates a chain of dependent operations, making it ideal for workflows where each step relies on the output of the previous step.

  • p-series:

    p-series executes promises in a strict sequence, ensuring that each promise is completed before the next one starts. This is essential for tasks that require a specific order of execution, such as data processing pipelines.

  • p-props:

    p-props takes an object of promises and resolves them concurrently, returning an object with the same keys but with the resolved values. This allows for structured data handling while still benefiting from concurrent execution.

Error Handling

  • p-all:

    p-all resolves with an array of results, but if any promise rejects, the returned promise will reject immediately with that error. This behavior is useful when you want to know if any of the concurrent operations failed without waiting for all to complete.

  • p-waterfall:

    p-waterfall will reject if any function in the chain returns a rejected promise. This allows for immediate error handling and prevents further execution of dependent operations.

  • p-series:

    p-series stops execution and rejects as soon as one promise fails, allowing you to handle errors immediately. This is beneficial when you want to halt a sequence of operations upon encountering an error.

  • p-props:

    p-props will reject if any of the promises in the object reject. This means that you can handle errors in a structured way, knowing exactly which promise failed based on the keys of the input object.

Use Cases

  • p-all:

    Best suited for scenarios where multiple independent asynchronous tasks need to be executed simultaneously, such as fetching data from multiple APIs at once.

  • p-waterfall:

    Great for workflows that require chaining of asynchronous operations, such as data transformations where each step depends on the output of the last.

  • p-series:

    Perfect for tasks that must be performed in a specific order, such as processing data step-by-step or performing database transactions that depend on the results of previous queries.

  • p-props:

    Ideal for cases where you have a set of asynchronous tasks that need to be resolved and organized by keys, such as loading user data or configuration settings.

Performance

  • p-all:

    p-all is optimized for concurrent execution, making it efficient for scenarios with many independent promises. However, it may lead to resource contention if too many promises are executed simultaneously.

  • p-waterfall:

    p-waterfall can introduce delays if each step in the chain takes time, but it provides a clear and manageable way to handle dependent asynchronous operations.

  • p-series:

    p-series can be slower due to its sequential nature, but it ensures that each operation completes before the next begins, which can be crucial for dependent tasks.

  • p-props:

    p-props balances concurrency with structured data management, making it efficient for resolving multiple promises while maintaining a clear mapping of results to keys.

Learning Curve

  • p-all:

    p-all is straightforward to use, especially for those familiar with promises. Its API is simple and intuitive, making it easy to integrate into existing codebases.

  • p-waterfall:

    p-waterfall may require a deeper understanding of promise chaining, but it provides a clear method for managing dependent asynchronous tasks, making it valuable for complex workflows.

  • p-series:

    p-series is easy to understand and implement, particularly for developers who are accustomed to synchronous programming patterns. Its sequential execution model is intuitive for most use cases.

  • p-props:

    p-props has a slightly higher learning curve due to its object-based structure, but it offers powerful capabilities for managing multiple promises in an organized way.

How to Choose: p-all vs p-waterfall vs p-series vs p-props
  • p-all:

    Choose p-all when you need to execute multiple promises concurrently and want to resolve when all of them have completed, regardless of their individual outcomes. It is ideal for scenarios where you want to wait for a batch of asynchronous operations to finish before proceeding.

  • p-waterfall:

    Use p-waterfall when you want to execute a series of asynchronous functions where each function receives the result of the previous one as its input. This is perfect for scenarios where you have a chain of dependent operations that need to be performed in a specific order.

  • p-series:

    Opt for p-series when you need to execute promises in a sequential manner, ensuring that each promise completes before the next one starts. This is particularly beneficial when the order of execution is crucial or when later promises depend on the results of earlier ones.

  • p-props:

    Select p-props when you want to resolve an object of promises and return an object with the same keys, but with the resolved values. This is useful for managing multiple asynchronous tasks that are keyed by identifiers, allowing for easier data organization and retrieval.

README for p-all

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 work in each function, use p-map instead.

See p-series for a serial counterpart.

Install

npm install p-all

Usage

import pAll from 'p-all';
import got from 'got';

const actions = [
	() => got('https://sindresorhus.com'),
	() => got('https://avajs.dev'),
	() => checkSomething(),
	() => doSomethingElse()
];

console.log(await pAll(actions, {concurrency: 2}));

API

pAll(tasks, options?)

Returns a Promise that is fulfilled when all promises returned from calling the functions in tasks are fulfilled, or rejects if any of the promises reject. The fulfilled value is an Array of the fulfilled values in tasks order.

tasks

Type: Iterable<Function>

Iterable with promise-returning/async functions.

options

Type: object

concurrency

Type: number
Default: Infinity
Minimum: 1

Number of concurrent pending promises.

stopOnError

Type: boolean
Default: true

When set to false, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an AggregateError containing all the errors from the rejected promises.

Related

  • p-map - Map over promises concurrently
  • p-series - Run promise-returning & async functions in series
  • p-props - Like Promise.all() but for Map and Object
  • p-queue - Promise queue with concurrency control
  • p-limit - Run multiple promise-returning & async functions with limited concurrency
  • More…