Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created November 30, 2014 00:58
Show Gist options
  • Save adamloving/61f04f61081b4cd7e8ad to your computer and use it in GitHub Desktop.
Save adamloving/61f04f61081b4cd7e8ad to your computer and use it in GitHub Desktop.
node process.nextTick()
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);
}
@adamloving
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment