Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Created February 16, 2014 05:59
Show Gist options
  • Save adohe-zz/9029896 to your computer and use it in GitHub Desktop.
Save adohe-zz/9029896 to your computer and use it in GitHub Desktop.
node.js event loop explanation
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