1

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.

Community
  • 1
  • 1
Jamex
  • 1,164
  • 5
  • 22
  • 34
  • "database.query" is not a standard part of JavaScript. Perhaps it is your database library making things async. – Diodeus - James MacFarlane Jun 20 '14 at 21:05
  • it depends on if .query() returns before or after the DB call gets back – dandavis Jun 20 '14 at 21:07
  • Is this a nodejs question? – jefflunt Jun 20 '14 at 21:07
  • 4
    It could very well be that `database.query(...)` runs asynchronously if it gets passed a callback and synchronously if not. However, why do you think it's not run asynchronously in the first case? How do you test that? The output of `console.log` alone doesn't tell you that. – Felix Kling Jun 20 '14 at 21:09
  • Hi, Yes, this question is from reading the nodebeginner.org post http://www.nodebeginner.org/#javascript-and-nodejs – Jamex Jun 20 '14 at 21:34
  • You will always need to read the documentation of the library you use. The doc will tell you if the function is sync, async or if it may change its behavior depending on the passed arguments. e.g. The `fs` module of nodejs has separate async and sync functions (e.g. `fs.exists`/`fs.existsSync`), while others may only have _sync_ or only _async_ functions. – t.niese Jun 20 '14 at 21:42

2 Answers2

0

When it comes to Javascript, sync vs async is more of a design pattern than a property of the functions themselves. A synchronous function will provide its results via its return value while an async function won't return any important values and willl instead provide its results by calling a callback with those values once they become available.

The difference is that if the function needs to wait for some system IO (like a database query), the synchronous version of the function will need to halt the Javascript execution until the database responds (so you can return the query results) while the async version will immediately return and resume Javascript execution as soon as the query is sent, arranging for the callback to get called when those results come back.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 1
    `[...]while an async function won't return any important values[...]` is not fully correct, as it could return a Promise object instead of expecting a callback function. – t.niese Jun 20 '14 at 21:16
  • A very good point! That said, in you are still going to need to pass a callback to the promise to get the query results so as far as "wait and return with values" vs "immedieately return and pass values to callback latter" promises work similarly to CPS style. – hugomg Jun 20 '14 at 21:22
0

I assume by not providing a callback, the function has this bit of node specifically behave in a synchronous manner.

Since it has to spawn a new thread for connecting to the database, in the first example where no callback is provided, the function will wait for that thread to finish it's thing before returning the result set.

In the second example, it will spawn the thread and return, and using whatever thread/event management methods of node/js, have the callback called as soon as the thread finishes.

So I guess a simplified answer:

Threading and choosing (as a programmer) whether or not to wait for the thread to finish before proceeding.

Josh
  • 140
  • 7
  • In the first instance, does having the database query return synchronously result in all requests being blocked in the meantime? – autodidact Jun 20 '14 at 21:22
  • It should block any future requests from being made, but previous asynchronous ones should still finish and run their callbacks I believe. But don't quote me, I've never tested something like that. – Josh Jun 20 '14 at 21:25