Created
November 30, 2014 00:58
-
-
Save adamloving/61f04f61081b4cd7e8ad to your computer and use it in GitHub Desktop.
node process.nextTick()
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
function doInner(i) { | |
console.log(i, 'doInner'); | |
process.nextTick(function() { | |
// runs after everything else is done | |
console.log(i, 'nextTick'); | |
}); | |
} | |
function doOuter(i) { | |
console.log(i, 'doOuter before inner'); | |
doInner(i); | |
console.log(i, 'doOuter after inner'); | |
} | |
for (var i = 0; i < 5; i++) { | |
doOuter(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All the functions in a given call tree will run before nextTick runs. Similarly, you should use process.nextTick to call callbacks, because if you call them directly, they aren't truly asynchronous (see example over here: http://howtonode.org/understanding-process-next-tick)