Created
February 2, 2012 06:48
-
-
Save yukidarake/1722062 to your computer and use it in GitHub Desktop.
node.jsとかで重めなループを回すと、イベントループがブロックされるのでいい感じに間にprocess.nextTickを挟んでくれるように
This file contains 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
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