Created
March 17, 2019 17:26
-
-
Save pejobo/88cefbb88829038a4d86b72c4a609a0d to your computer and use it in GitHub Desktop.
node / java script: perform a list of tasks with limit parallelism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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