Skip to content

Instantly share code, notes, and snippets.

@Chris927
Created July 11, 2014 13:38
Show Gist options
  • Select an option

  • Save Chris927/a9e5f6e458700fb976cf to your computer and use it in GitHub Desktop.

Select an option

Save Chris927/a9e5f6e458700fb976cf to your computer and use it in GitHub Desktop.
// some dummy rows
var rows = [];
for (var i = 0; i < 10000; i++) {
rows.push("this is row " + i);
}
var index = 0;
var currentlyRunning = 0, maxRunning = 200;
function callHandler(row, cb) {
console.log("callHandler for row: " + row);
setTimeout(function() {
cb(null);
}, 500 * Math.random());
}
function printStack() {
var e = new Error('dummy');
var stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '')
.replace(/^\s+at\s+/gm, '')
.replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@')
.split('\n');
console.log(stack);
}
function run(ix) {
console.log("run " + ix + ", currentlyRunning=" + currentlyRunning);
if (ix % 50 == 0) printStack(); // just curious to see the stack from time to time
currentlyRunning++;
callHandler(rows[ix], function(err) {
currentlyRunning--;
if (index < rows.length && currentlyRunning < maxRunning) {
run(index++);
}
});
};
while (index < rows.length && currentlyRunning < maxRunning) {
run(index++);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment