What is p-waterfall?
The p-waterfall npm package allows you to run promise-returning & async functions in series, each passing its result to the next. It is useful for scenarios where you need to perform a series of asynchronous operations in a specific order, with each operation depending on the result of the previous one.
What are p-waterfall's main functionalities?
Running a series of promise-returning functions
This feature allows you to run a series of promise-returning functions in sequence. Each function receives the result of the previous function as its input. In this example, the initial value is 5, and the series of operations results in 9.
const pWaterfall = require('p-waterfall');
const tasks = [
initialValue => Promise.resolve(initialValue + 1),
previousValue => Promise.resolve(previousValue * 2),
previousValue => Promise.resolve(previousValue - 3)
];
pWaterfall(tasks, 5).then(result => {
console.log(result); // 9
});
Running a series of async functions
This feature allows you to run a series of async functions in sequence. Each function receives the result of the previous function as its input. In this example, the initial value is 5, and the series of operations results in 9.
const pWaterfall = require('p-waterfall');
const tasks = [
async initialValue => initialValue + 1,
async previousValue => previousValue * 2,
async previousValue => previousValue - 3
];
pWaterfall(tasks, 5).then(result => {
console.log(result); // 9
});
Other packages similar to p-waterfall
async
The async package provides a wide range of utilities for working with asynchronous JavaScript. It includes a waterfall method that is similar to p-waterfall, allowing you to run a series of asynchronous functions in sequence. However, async offers a broader set of utilities beyond just the waterfall pattern.
promise-waterfall
The promise-waterfall package is another utility for running promise-returning functions in sequence. It is similar to p-waterfall in that it focuses on the waterfall pattern, but it may have a different API and fewer features compared to p-waterfall.
bluebird
Bluebird is a fully-featured promise library that includes a method called .reduce, which can be used to achieve a similar effect to p-waterfall by reducing an array of functions into a single promise chain. Bluebird offers many other features and utilities for working with promises.
p-waterfall
Run promise-returning & async functions in series, each passing its result to the next
Install
$ npm install p-waterfall
Usage
import pWaterfall from 'p-waterfall';
const tasks = [
initialValue => getEmoji(initialValue),
previousValue => `I ❤️ ${previousValue}`
];
console.log(await pWaterfall(tasks, 'unicorn'));
API
pWaterfall(tasks, initialValue?)
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 the value returned from the last task.
tasks
Type: Iterable<Function>
Functions are expected to return a value. If a Promise
is returned, it's awaited before continuing with the next task.
initialValue
Type: unknown
Value to use as previousValue
in the first task.
Related