ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between synchronous and asynchronous programming (in node.js)

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.

Stange you didn't find anything with google, it's a rather big subject. In synchronous programming, each step is performed one after the previous one is finished executing. In asynchroneous, step 2 will be performed even if step 1 isn't finished. The function you see defined in your second example is called a callBack function, and will be ran as soon as the result from the database will be returned, which will probably be after the console.log is ran.
@Bartdude There was a lot on asynchronous programming, but no somewhat simple explanation on what it is, and what it means in practice.
@GabrielLlamas Why should we avoid synchronous functions?
@CharlieParker Because they block the event loop and you're losing all the benefits from an asynchronous evented I/O model. And because it's a bad practice. Think about it this way: If you're not using asynchronous functions, why are you using Node.js?
@GabrielLlamas, if I'm executing an INSERT query and I want to use the last inserted ID after database.query(), then I should call it as synchronously, right? or what should be the approach? (This question I have for long time)

C
Community

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:

Query finished Next line 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


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;
@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.
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.
@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?
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.
p
pravj

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

If your operations are not doing very heavy lifting like querying huge data from DB then go ahead with Synchronous way otherwise Asynchronous way. 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.


Does that mean that db.query(cmd, callback) is running concurrently (as in threads)? Are they running at the same time?
In his second example, is there any chance that the query finish so fast that it then calls the callback first, before console.log?
@Fahmi theoretically yes, practically quite impossible
M
Martijn

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.


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 :)
@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...
J
Jay

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.


r
related

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.


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?
This depends on the processor (is it multi-core?) and the operating system. See en.wikipedia.org/wiki/Multithreading_(software)#Multithreading
t
thebreiflabb

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.


W
Willem van der Veen

Asynchronous programming in JS:

Synchronous

Stops execution of further code until this is done.

Because it this stoppage of further execution, synchronous code is called 'blocking'. Blocking in the sense that no other code will be executed.

Asynchronous

Execution of this is deferred to the event loop, this is a construct in a JS virtual machine which executes asynchronous functions (after the stack of synchronous functions is empty).

Asynchronous code is called non blocking because it doesn't block further code from running.

Example:

// This function is synchronous function log(arg) { console.log(arg) } log(1); // This function is asynchronous setTimeout(() => { console.log(2) }, 0); log(3)

The example logs 1, 3, 2.

2 is logged last because it is inside a asynchronous function which is executed after the stack is empty.


A
Anton Chan

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).


佚名

Synchronous functions are blocking while asynchronous functions are not. In synchronous functions, statements complete before the next statement is run. In this case, the program is evaluated exactly in order of the statements and execution of the program is paused if one of the statements take a very long time.

Asynchronous functions usually accept a callback as a parameter and execution continue on the next line immediately after the asynchronous function is invoked. The callback is only invoked when the asynchronous operation is complete and the call stack is empty. Heavy duty operations such as loading data from a web server or querying a database should be done asynchronously so that the main thread can continue executing other operations instead of blocking until that long operation to complete (in the case of browsers, the UI will freeze).

Orginal Posted on Github: Link


N
Neeraj Bansal

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.