The classic way to start sequential asynchronous processes is to use async.js and use async.series() . If you prefer promise-based code, there is an impoverished version of async.js : async-q
With async-q you can use series again:
async.series([ function(){return delayPromise(100, "a:a")}, function(){return delayPromise(100, "a:b")}, function(){return delayPromise(100, "a:c")} ]) .then(function(){ console.log(done); });
Running two of them at the same time will start a and b at the same time, but inside each of them they will be sequential:
// these two will run concurrently but each will run // their array of functions sequentially: async.series(a_array).then(()=>console.log('a done')); async.series(b_array).then(()=>console.log('b done'));
If you want to run b after a , put it in .then() :
async.series(a_array) .then(()=>{ console.log('a done'); return async.series(b_array); }) .then(()=>{ console.log('b done'); });
If, instead of starting sequentially in series, you want to limit them to running a certain number of processes simultaneously, you can use parallelLimit() :
// Run two promises at a time: async.parallelLimit(a_array,2) .then(()=>console.log('done'));
Read the async-q docs: https://github.com/dbushong/async-q/blob/master/READJSME.md
source share