Skip to content

Instantly share code, notes, and snippets.

@flockonus
Last active January 30, 2018 21:11
Show Gist options
  • Save flockonus/4a22eda05435be00c209e1183437a111 to your computer and use it in GitHub Desktop.
Save flockonus/4a22eda05435be00c209e1183437a111 to your computer and use it in GitHub Desktop.
const Bluebird = require('bluebird');
// https://github.com/charto/cwait
const { TaskQueue } = require('cwait');
// https://github.com/ForbesLindesay/throat
const throat = require('throat')(Bluebird);
const list = new Array(300).fill(0).map((x, i) => i);
async function job(i) {
console.log('>', i, new Date());
return new Promise(function(resolve) {
setTimeout(resolve, i % 2 === 0 ? 1 : 100, i);
});
}
async function run1() {
var queue = new TaskQueue(Bluebird, 3);
const res = await Bluebird.map(list, queue.wrap(job));
console.log('>>', res);
}
async function run2() {
// const queue = throat(3);
const res = await Promise.all(list.map(throat(3, x => job(x))));
console.log('>>', res);
}
// TODO another one with bluebird?
run1().then(console.log, console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment