Created
April 15, 2015 16:40
-
-
Save danared/60eac629c1b55d88c29d to your computer and use it in GitHub Desktop.
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 foo() { | |
someAsyncFunction(params, function(err, results) { | |
console.log(“one”); | |
}); | |
console.log(“two”); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the tutorial:
"In the above example, we may think that the output would be:
one two
but in fact it might betwo one
because the line that prints “one” might happen later, asynchronously, in the callback. We say “might” because if conditions are just right, “one” might print before “two”."Am I wrong in thinking in this example the conditions will never be "just right"?
The way the JS job queue works the asynchronous task will go to the end of the queue whereas the execution of foo() would already be queued, so no matter what
someAsyncFunction
does, it will always happen after the execution of line 5.As a minimal reproduction case, this will always print
two one
: