Skip to content

Instantly share code, notes, and snippets.

@pejobo
Created March 17, 2019 17:26
Show Gist options
  • Save pejobo/88cefbb88829038a4d86b72c4a609a0d to your computer and use it in GitHub Desktop.
Save pejobo/88cefbb88829038a4d86b72c4a609a0d to your computer and use it in GitHub Desktop.
node / java script: perform a list of tasks with limit parallelism
async function doConcurrent(parallelity, tasks) {
const promises = [];
const chain = async (f1, f2) => {
await f1();
await f2();
};
const next = () => {
if (tasks.length) {
// console.log('start next, task count is ' + tasks.length);
var task = tasks.shift();
promises.push(chain(task, next));
}
}
const initial = tasks.slice(0, parallelity);
tasks = tasks.slice(parallelity);
initial.forEach(t => promises.push(chain(t, next)));
while (tasks.length) {
// console.log('await all, promise count is ' + promises.length);
await Promise.all(promises);
}
await Promise.all(promises);
// console.log('everything finished');
}
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
});
}
async function task(name) {
console.log('start ' + name + '..');
await sleep(500 + Math.floor(Math.random() * 2000));
console.log('...finished ' + name);
}
await doConcurrent(10, [() => task('test')])
await doConcurrent(2, [1,2,3,4,5].map(v => () => task(v)));
// line 42 reserved for answer to big question
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment