Skip to content

Instantly share code, notes, and snippets.

@Olegas
Forked from kvasdopil/files.js
Last active July 4, 2017 12:17
Show Gist options
  • Save Olegas/f181b05700f1d84459b10135e0b715ca to your computer and use it in GitHub Desktop.
Save Olegas/f181b05700f1d84459b10135e0b715ca to your computer and use it in GitHub Desktop.
const files = [/* file names here */];
const limit = 100;
async function main() {
var runningWorkers = 0;
function continuationHandler() {
runningWorkers--;
loadWorkers();
}
function pickItem() {
const file = files.shift();
if (file) {
runningWorkers++;
// continue in any case
worker(file).then(continuationHandler, continuationHandler);
}
}
function loadWorkers() {
do {
pickItem();
} while(runningWorkers < limit)
}
loadWorkers();
}
async function worker(file) {
return new Promise(done => {
// process file and call done at the end
// alternatively use async/await file functions
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment