const Promise = require('bluebird');
const batch = async (args, callback, options = {}) => {
const { max = 2 } = options;
const results = [];
let running = args.slice(0, max).map(a => callback(...a));
let picked = running.length;
while (running.length > 0) {
results.push(await Promise.race(running));
const stillRunning = running.filter(p => p.isPending());
const done = running.length - stillRunning.length;
const next = args.slice(picked, picked + Math.min(done, max)).map(a => callback(...a));
picked += next.length;
running = stillRunning.concat(next);
}
return results;
};
const impl = (f) => {
console.log('starting', f);
return new Promise((resolve, reject) => setTimeout(() => {
console.log(f);
if (f === "file2") reject("oups");
else resolve();
}, 1000));
};
batch([["file1"], ["file2"], ["file3"]], impl, { max: 2 });