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

前端 未结 10 1400
情歌与酒
情歌与酒 2020-11-22 01:49

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

The first one:

    var result = database.query(\"SELECT * FROM hu         


        
相关标签:
10条回答
  • 2020-11-22 02:18

    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.

    0 讨论(0)
  • 2020-11-22 02:24

    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.

    0 讨论(0)
  • 2020-11-22 02:25

    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.

    0 讨论(0)
  • 2020-11-22 02:31

    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.

    0 讨论(0)
提交回复
热议问题