By Azeirah


2013-05-02 11:00:47 8 Comments

I've been reading nodebeginner And I came across the following two pieces of code.

The first one:

    var result = database.query("SELECT * FROM hugetable");
    console.log("Hello World");

The second one:

    database.query("SELECT * FROM hugetable", function(rows) {
       var result = rows;
    });
    console.log("Hello World");

I get what they're supposed to do, they query the database to retrieve the answer to the query. And then console.log('Hello world').

The first one is supposedly synchronous code. And the second one is asynchronous code.

The difference between the two pieces is very vague to me. What would the output be?

Googling on asynchronous programming didn't help me either.

8 comments

@Neeraj Bansal 2018-02-07 12:21:59

Sync Programming

Programming languages like C, C#, Java are sync programming, what so ever you write will be execute in order of your writing.

-GET DATA FROM SQL.
//Suppose fetching data take 500 msec

-PERFORM SOME OTHER FUNCTION.
//Performing some function other will take 100 msec, but execution of other 
//task start only when fetching of sql data done (i.e some other function 
//can execute only after first in process job finishes).

-TOTAL TIME OF EXECUTION IS ALWAYS GREATER THAN (500 + 100 + processing time) 
msec

Async

NodeJs comes up with async feature, it's non-blocking in nature, suppose in any I/O task which is taking time (fetching, writing, reading), nodejs won't kept idle and wait for the task to be finish, it'll start executing next tasks in the queue, and whenever that time taking task completed it will notify using callback. Following example will help:

//Nodejs uses callback pattern to describe functions.
//Please read callback pattern to understand this example

//Suppose following function (I/O involved) took 500 msec
function timeConsumingFunction(params, callback){
  //GET DATA FROM SQL
  getDataFromSql(params, function(error, results){
    if(error){
      callback(error);
    }
    else{
      callback(null, results);
    }
  })
}

//Suppose following function is non-blocking and took 100 msec
function someOtherTask(){
  //some other task
  console.log('Some Task 1');
  console.log('Some Task 2');
}

console.log('Execution Start');

//Start With this function
timeConsumingFunction(params, function(error, results){
    if(error){
      console.log('Error')
    }
    else{
      console.log('Successfull'); 
    }
  })

//As (suppose) timeConsumingFunction took 500 msec, 
//As NodeJs is non-blocking, rather than remain idle for 500 msec, it will start 
//execute following function immediately
someOtherTask();

In Short, Output is as:

Execution Start
//Roughly after 105 msec (5 msec it'll take in processing)
Some Task 1
Some Task 2
//Roughly After 510 msec
Error/Successful //depends on success and failure of DB function execution

Difference is clear where sync will definitely take more than 600 (500 + 100 + processing time) msec, async saves time.

@Salvatorelab 2013-05-02 11:03:00

The difference is that in the first example, the program will block in the first line. The next line (console.log) will have to wait.

In the second example, the console.log will be executed WHILE the query is being processed. That is, the query will be processed in the background, while your program is doing other things, and once the query data is ready, you will do whatever you want with it.

So, in a nutshell: The first example will block, while the second won't.

The output of the following two examples:

// Example 1 - Synchronous (blocks)
var result = database.query("SELECT * FROM hugetable");
console.log("Query finished");
console.log("Next line");


// Example 2 - Asynchronous (doesn't block) 
database.query("SELECT * FROM hugetable", function(result) {
    console.log("Query finished");
});
console.log("Next line");

Would be:

  1. Query finished
    Next line
  2. Next line
    Query finished

Note
While Node itself is single threaded, there are some task that can run in parallel. For example, File System operations occur in a different process.

That's why Node can do async operations: one thread is doing file system operations, while the main Node thread keeps executing your javascript code. In an event-driven server like Node, the file system thread notifies the main Node thread of certain events such as completion, failure, or progress, along with any data associated with that event (such as the result of a database query or an error message) and the main Node thread decides what to do with that data.

You can read more about this here: How the single threaded non blocking IO model works in Node.js

@Azeirah 2013-05-02 11:04:49

So basically, when I execute the first piece of code, it will do something like this: request query.; 5 seconds later when the request is done; console.log; when the second one executes: request query; console.log; work on the query;

@Salvatorelab 2013-05-02 11:08:20

yep, that's it.

@JohnGalt 2013-10-20 17:08:09

@TheBronx Doesn't the query method have to be written in an "asynchronous way" or is the sql query run on a different process (non-node thread) so that's why it doesn't block the node thread?

@Salvatorelab 2013-10-21 07:36:11

@JohnGalt the sql runs on a different thread. But of course that depends on the implementation of the sql driver you use. The driver should spawn a new thread, connect to mysql and run the query. Once done, post the result to the event queue, and Node will call the callback.

@greatwolf 2013-11-19 22:44:59

Isn't it possible for the async example to output the same thing as #1? Like for instance, database.query finishes so fast that by the time we reach console.log the task is already done.

@Abdul 2016-03-06 01:39:15

@TheBronx if console.log("Next line"); in example 2 was inside the anonymous function, so right after console.log("query finished");, that would mean "Next Line" would be printed AFTER "query finished" right? So, if I have everything in a nested fashion, everything would run in a synchronous fashion, thus I wouldn't need to worry about using synchronous versions of certain functions. Am I correct in my understanding?

@Salvatorelab 2016-03-06 10:06:09

Short answer: Yes @Abdul, you are right. Long answer: Nesting functions (callbacks) is the way to do things sequentially, "one after each other". But that is not "synchronous" technically. The anonymous function is still executed "when the blocking operation has finished", or in other words, "asynchronously". Node.js could execute other functions while that blocking operation is taking place. Functions remain async, its just that you are chaining them. Sync functions block execution, that's the key.

@Abdul 2016-03-06 10:07:41

@TheBronx Ah ok, got it, thanks.

@tatsu 2017-09-19 14:56:09

I get the exact opposite. this does not explain why I get the exact opposite.

@Salvatorelab 2017-09-19 17:38:55

@tatsu is your async call really asynchronous? a callback parameter does not mean the function is asynchronous, it just means it executes the callback when completed, sync or async.

@tatsu 2017-09-20 07:25:23

I guess it's because it's only the setState() part that's asynchronous. (I'm doing a react project)

@Jay 2015-08-08 12:31:26

First, I realize I am late in answering this question.

Before discussing synchronous and asynchronous, let us briefly look at how programs run.

In the synchronous case, each statement completes before the next statement is run. In this case the program is evaluated exactly in order of the statements.

This is how asynchronous works in JavaScript. There are two parts in the JavaScript engine, one part that looks at the code and enqueues operations and another that processes the queue. The queue processing happens in one thread, that is why only one operation can happen at a time.

When an asynchronous operation (like the second database query) is seen, the code is parsed and the operation is put in the queue, but in this case a callback is registered to be run when this operation completes. The queue may have many operations in it already. The operation at the front of the queue is processed and removed from the queue. Once the operation for the database query is processed, the request is sent to the database and when complete the callback will be executed on completion. At this time, the queue processor having "handled" the operation moves on the next operation - in this case

    console.log("Hello World"); 

The database query is still being processed, but the console.log operation is at the front of the queue and gets processed. This being a synchronous operation gets executed right away resulting immediately in the output "Hello World". Some time later, the database operation completes, only then the callback registered with the query is called and processed, setting the value of the variable result to rows.

It is possible that one asynchronous operation will result in another asynchronous operation, this second operation will be put in the queue and when it comes to the front of the queue it will be processed. Calling the callback registered with an asynchronous operation is how JavaScript run time returns the outcome of the operation when it is done.

A simple method of knowing which JavaScript operation is asynchronous is to note if it requires a callback - the callback is the code that will get executed when the first operation is complete. In the two examples in the question, we can see only the second case has a callback, so it is the asynchronous operation of the two. It is not always the case because of the different styles of handling the outcome of an asynchronous operation.

To learn more, read about promises. Promises are another way in which the outcome of an asynchronous operation can be handled. The nice thing about promises is that the coding style feels more like synchronous code.

Many libraries like node 'fs', provide both synchronous and asynchronous styles for some operations. In cases where the operation does not take long and is not used a lot - as in the case of reading a config file - the synchronous style operation will result in code that is easier to read.

@Santosh Panda 2013-05-02 11:19:06

The difference between these two approaches is as follows:

Synchronous way: It waits for each operation to complete, after that only it executes the next operation. For your query: The console.log() command will not be executed until & unless the query has finished executing to get all the result from Database.

Asynchronous way: It never waits for each operation to complete, rather it executes all operations in the first GO only. The result of each operation will be handled once the result is available. For your query: The console.log() command will be executed soon after the Database.Query() method. While the Database query runs in the background and loads the result once it is finished retrieving the data.

Use cases

  1. If your operations are not doing very heavy lifting like querying huge data from DB then go ahead with Synchronous way otherwise Asynchronous way.

  2. In Asynchronous way you can show some Progress indicator to the user while in background you can continue with your heavy weight works. This is an ideal scenario for GUI apps.

@Charlie Parker 2014-02-09 00:41:51

Does that mean that db.query(cmd, callback) is running concurrently (as in threads)? Are they running at the same time?

@Fahmi 2016-11-18 07:12:48

In his second example, is there any chance that the query finish so fast that it then calls the callback first, before console.log?

@Anton Chan 2015-02-17 19:37:26

The function makes the second one asynchronous.

The first one forces the program to wait for each line to finish it's run before the next one can continue. The second one allows each line to run together (and independently) at once.

Languages and frameworks (js, node.js) that allow asynchronous or concurrency is great for things that require real time transmission (eg. chat, stock applications).

@Martijn 2013-05-02 11:08:11

This would become a bit more clear if you add a line to both examples:

var result = database.query("SELECT * FROM hugetable");
console.log(result.length);
console.log("Hello World");

The second one:

database.query("SELECT * FROM hugetable", function(rows) {
   var result = rows;
   console.log(result.length);
});
console.log("Hello World");

Try running these, and you’ll notice that the first (synchronous) example, the result.length will be printed out BEFORE the 'Hello World' line. In the second (the asynchronous) example, the result.length will (most likely) be printed AFTER the "Hello World" line.

That's because in the second example, the database.query is run asynchronously in the background, and the script continues straightaway with the "Hello World". The console.log(result.length) is only executed when the database query has completed.

@humanityANDpeace 2014-01-18 16:50:01

you say: the result.length will (most likely) be printed AFTER the "Hello World" line. .... why would that be only "most likely"? I think it is always printed after the console.log output. Thanks for clarification :)

@Martijn 2014-01-19 10:12:43

@humanityANDpeace: that's the entire point of asynchronous access: you don't know when it will be done. Perhaps it's an absurdly fast database, and the database query returns even before Javascript gets to the "Hello World" line...

@related 2013-05-02 11:04:30

In the synchronous case, the console.log command is not executed until the SQL query has finished executing.

In the asynchronous case, the console.log command will be directly executed. The result of the query will then be stored by the "callback" function sometime afterwards.

@Charlie Parker 2014-02-09 01:03:57

But are the actually being called simultaneously? The thing that confuses me is, in asynchronous code, is the actual code being run at the same time in parallel?

@related 2014-02-17 10:02:57

This depends on the processor (is it multi-core?) and the operating system. See en.wikipedia.org/wiki/Multithreading_(software)#Multithreadi‌​ng

@thebreiflabb 2013-05-02 11:03:08

The main difference is with asynchronous programming, you don't stop execution otherwise. You can continue executing other code while the 'request' is being made.

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] Asynchronous vs synchronous execution, what does it really mean?

31 Answered Questions

17 Answered Questions

21 Answered Questions

[SOLVED] What is the difference between call and apply?

33 Answered Questions

[SOLVED] What's the difference between using "let" and "var"?

12 Answered Questions

12 Answered Questions

[SOLVED] What is the purpose of Node.js module.exports and how do you use it?

  • 2011-03-15 11:56:10
  • mrwooster
  • 464377 View
  • 1390 Score
  • 12 Answer
  • Tags:   javascript node.js

8 Answered Questions

[SOLVED] What is the difference between Bower and npm?

  • 2013-09-05 16:53:19
  • Games Brainiac
  • 309157 View
  • 1714 Score
  • 8 Answer
  • Tags:   javascript npm bower

13 Answered Questions

[SOLVED] Check synchronously if file/directory exists in Node.js

  • 2010-12-19 11:19:43
  • Ragnis
  • 698164 View
  • 1095 Score
  • 13 Answer
  • Tags:   node.js

14 Answered Questions

Sponsored Content