Skip to content

Instantly share code, notes, and snippets.

@nathanmarks
Created November 17, 2016 01:05
Show Gist options
  • Save nathanmarks/c2d57dfaf171248f4da27c097645ac90 to your computer and use it in GitHub Desktop.
Save nathanmarks/c2d57dfaf171248f4da27c097645ac90 to your computer and use it in GitHub Desktop.
concurrent generator
import Promise from 'bluebird';
import map from 'lodash/fp/map';
/**
* @module utils/concurrentGenerator
*/
export default function ConcurrentGenerator({
concurrency = 5,
items = [],
operation = () => {},
}) {
function* generator() {
const ret = [];
for (let i = 0; i < items.length; i++) {
ret[i] = yield operation(items[i]);
}
return ret;
}
return runGenerator.bind(null, generator, concurrency);
}
function runGenerator(generator, concurrency) {
return new Promise((resolve) => {
const it = generator();
const iteratees = [];
function iterate() {
const iterator = it.next();
if (!iterator.done) {
iteratees.push(iterator);
return iterator.value.then(() => iterate());
} else {
return Promise.all(map((n) => n.value, iteratees))
.then((result) => resolve(result));
}
}
for (let i = 0; i < concurrency; i++) {
iterate();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment