Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created December 2, 2014 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/9b34ff40defce6c1e288 to your computer and use it in GitHub Desktop.
Save adamloving/9b34ff40defce6c1e288 to your computer and use it in GitHub Desktop.
Another process.nextTick example that shows callbacks aren't intrinically asynchronous.
function doSomething(i, callback) {
console.log(i, 'doSomething before callback');
// this is in fact synchronous, since don't use nextTick
callback();
console.log(i, 'doSomething after callback');
process.nextTick(function() {
// you might think this yeilds to the next "doSomething",
// but in fact it runs after everything else is done
console.log(i, 'nextTick');
});
}
function onDone() {
console.log(i, 'done');
}
for (var i = 0; i < 5; i++) {
doSomething(i, onDone);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment