Created
July 11, 2014 13:38
-
-
Save Chris927/a9e5f6e458700fb976cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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