I want to ask what makes a javascript function asynchronous.
This question is similar to a question asked here,
//example 1 var result = database.query("SELECT * FROM hugetable"); console.log("query finished"); console.log("Next line"); //example 2 database.query("SELECT * FROM hugetable", function(rows) { console.log("query finished"); }); console.log("Next line");
Output would be:
Example 1
query finished
Next line
Example 2
Next line
query finished
I don't see the reason (the mechanism) why example 1 is synchronous, but example 2 is asynchronous. Put it another way, both examples have the same query statement, why is it that the console.log statements in example 1 have to wait for the query to complete before they get executed. But in example 2, the "next line" statement is allowed to be executed before the database query is completed.