Skip to content

Instantly share code, notes, and snippets.

@yukidarake
Created February 2, 2012 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukidarake/1722062 to your computer and use it in GitHub Desktop.
Save yukidarake/1722062 to your computer and use it in GitHub Desktop.
node.jsとかで重めなループを回すと、イベントループがブロックされるのでいい感じに間にprocess.nextTickを挟んでくれるように
function loop(fn, times, unit) {
unit = unit || 10;
function _loop(n) {
for (var i = n; i < n + unit; i++) {
fn(i);
if (i === times - 1) {
return;
}
}
process.nextTick(function() {
_loop(n + unit);
});
}
_loop(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment