Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created September 27, 2018 11:50
Show Gist options
  • Save lsongdev/8afe6a920de6c2bcbeccee161a334cfe to your computer and use it in GitHub Desktop.
Save lsongdev/8afe6a920de6c2bcbeccee161a334cfe to your computer and use it in GitHub Desktop.
const tasks = [
1,1,1,1,,1,1,1,1
,1,1,1,2,2,2,2,3,
3,,3,43,4343,43,
,43,4,3,4,34,3,4,3
]
.filter(Boolean)
.map(t => cb => {
setTimeout(cb, 1000, 1,2);
});
const parallelLimit = (tasks, limit, done) => {
let running = 0;
let results = [];
(function run(){
while(running < limit && tasks.length){
running++;
const task = tasks.shift();
task((err, res) => {
running--;
results.push({ res, err });
if(tasks.length){
run();
}else{
done(results);
}
});
}
})();
};
parallelLimit(tasks, 5, r => {
console.log(r);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment