Created
December 2, 2014 19:03
-
-
Save adamloving/9b34ff40defce6c1e288 to your computer and use it in GitHub Desktop.
Another process.nextTick example that shows callbacks aren't intrinically asynchronous.
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
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