Created
September 27, 2018 11:50
-
-
Save lsongdev/8afe6a920de6c2bcbeccee161a334cfe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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