Created
February 16, 2014 05:59
-
-
Save adohe-zz/9029896 to your computer and use it in GitHub Desktop.
node.js event loop explanation
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
The event loop cycle is timers -> I/O -> immediates, rinse and repeat. But that when you | |
haven't entered the event loop, then timers come first-but only on the first tick. timers | |
are based on a time in the future, even if it's 0, while check immediate is always on the | |
next turn of the loop. So it's possible that the delay of the event loop is low enough for | |
the timer to fire after the immediate. | |
setTimeout(function() { | |
console.log('setTimeout'); | |
}, 0); | |
setImmediate(function() { | |
console.log('setImmediate'); | |
}); | |
setTimeout(function() { | |
setTimeout(function() { | |
console.log('setTimeout') | |
}, 0); | |
setImmediate(function() { | |
console.log('setImmediate') | |
}); | |
}, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment