Sequentially run promise-returning functions, passing the result of each one to its next
npm install promises-pipeline --save
tasks
{Iterable<Function>}
An iterable of promise-returning functionsinitialValue
{any}
An optional initial value of any type
pipeline()
returns a promise that resolves when all of the promises in the given functions have resolved or, if any of the promises rejects, rejects immediately with the reason of the first promise that rejected, discarding all the other promises whether or not they have resolved.
const pipeline = require('promises-pipeline');
const errorTask = () => Promise.reject(new Error('Failed generating "Hello, World!"'));
const helloTask = () => Promise.resolve('Hello,');
const hiTask = input => Promise.resolve(`Hi${input}`);
const worldTask = input => Promise.resolve(`${input} World!`);
const helloWorldTasks = new Map();
helloWorldTasks.set('helloTask', helloTask);
helloWorldTasks.set('worldTask', worldTask);
const hiWorldTasks = new Set();
hiWorldTasks.add(hiTask);
hiWorldTasks.add(worldTask);
const failureTasks = [helloTask, errorTask, worldTask];
pipeline(helloWorldTasks.values())
.then(output => console.log(output))// => 'Hello, World!'
.catch(err => console.error(err.message));
pipeline(hiWorldTasks, ',')
.then(output => console.log(output))// => 'Hi, World!'
.catch(err => console.error(err.message));
pipeline(failureTasks)
.then(output => console.log(output))
.catch(err => console.error(err.message));// => 'Failed generating "Hello, World!"'
npm test
This project is licensed under the MIT license. See the LICENSE file for more info.