promises-runner
Run promises from an array of objects waiting for some promises to be resolved before continuing running others
Install
npm install --save promises-runner
Api
objectsArrayWithPromises single object should be
{promise: (inputData) => Promise, wait: true|false, outputKey: string}
logger function
logger: (action: String, objectFromArrayWithPromises: Object, inputOutputOrError: Mixed)
action
= START|ERROR|DONE
objectFromArrayWithPromises
= one object from objectsArrayWithPromises
with (promiseName
)
inputOutputOrError
= would be input of promise if action = START,
output of promise if action = DONE
and error if action = ERROR
Check example logger function at end of README
Usage example
var PromisesRunner = { return Promise}const objectsArrayWithPromises = promise: expensiveOperation // <--┐ promise: expensiveOperation // <----- these will run in parallel promise: expensiveOperation // <--┘ promise: expensiveOperation wait: true // this will wait before all previous are resolved promise: expensiveOperation // these 2 -- after the previous one is resolved promise: expensiveOperation // these 2 -- and are ran in parallel promise: expensiveOperation wait: true // this will wait before all previous are resolved promise: expensiveOperation // these 2 -- after the previous one is resolved promise: expensiveOperation // these 2 -- and are ran in parallelvar pr = objectsArrayWithPromises; prstart; // pause/resume PromisesRunnerconst resume = pr;; // stop promises runner,// this will send currentOutput and// promise.then will be called after completion of promises already running (parallel ones)const currentOutput = pr;
Example of objects being merged as per outputKey and mergePromiseOutputToNextPromiseInput
const sleepFor = ;const allPromises = wait: true wait: true wait: true outputKey: 'abc' ; objectsArrayWithPromises: allPromises inputData: someInputData: -1 outputDataKey: 'outputKey' // default false mergePromiseOutputToNextPromiseInput: true // default false start /* for outputDataKey = 'outputKey', mergePromiseOutputToNextPromiseInput = trueinput for all promises: {someInputData: -1, outputKey: {a1: 'action1', a2: 'action2' ....}}output: { outputKey: { a1: 'action1', a2: 'action2', a3: 'action3w', a4: 'action4', a5: 'action5', a6: 'action6w', a7: 'action7', a8: 'action8w', a9: 'action9', abc: { a10: 'action10' } }}*//* for outputDataKey = false, mergePromiseOutputToNextPromiseInput = trueinput for all promises: {someInputData: -1, a1: 'action1', a2: 'action2' ....}output: { a1: 'action1', a2: 'action2', a3: 'action3w', a4: 'action4', a5: 'action5', a6: 'action6w', a7: 'action7', a8: 'action8w', a9: 'action9', abc: { a10: 'action10' }}*//* for default outputDataKey = false, mergePromiseOutputToNextPromiseInput = falseinput for all promises: {someInputData: -1}output: { a1: 'action1', a2: 'action2', a3: 'action3w', a4: 'action4', a5: 'action5', a6: 'action6w', a7: 'action7', a8: 'action8w', a9: 'action9', abc: { a10: 'action10' }}*/
Example logger
taskLogger.js
const chalk = ;const lowerCase = ;const pad = ;module { let consoleColor = chalk; console;};