Skip to content

Instantly share code, notes, and snippets.

@nathanmarks
Created April 22, 2016 20:13
Show Gist options
  • Save nathanmarks/e9bc45b6086c9651c514270794a6a3f9 to your computer and use it in GitHub Desktop.
Save nathanmarks/e9bc45b6086c9651c514270794a6a3f9 to your computer and use it in GitHub Desktop.
import Promise from 'bluebird';
import map from 'lodash/fp/map';
/**
* @module utils/concurrentGenerator
*/
export default function ConcurrentGenerator ({
concurrency = 5,
items = [],
operation = () => {}
}) {
function* generator () {
let 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 = [];
// asynchronously iterate over generator
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