Node. js Study Notes (2) -- about asynchronous programming style
The asynchronous programming style of Node. js is a major feature of it. In code, it is reflected in callback. First, the code is executed sequentially: function heavyCompute (n, callback) {var count = 0, I, j; for (I = n; I> 0; -- I) {for (j = n; j> 0; -- j) {count + = 1 ;}} callback (count) ;}heavycompute (10000, function (count) {console. log (count) ;}); console. log ('hello'); the output here is 100000000 hello, which indicates that only one function can be executed at a time, even if it takes a long time, one by one. However, there are two interesting examples: 1. setTimeout (function () {console. log ('World');}, 1000); console. log ('hello'); Output helloworld. We can understand that in the thread of sequential execution, if a function is set to timeout, a parallel thread will be created to return immediately, then let the main js thread execute the subsequent code, and then execute the callback function after receiving the notification from this parallel thread. The result is "helloworld" instead of "worldhello" almost simultaneously after 1 s. 2. The following is a typical scenario: function heavyCompute (n) {var count = 0, I, j; for (I = n; I> 0; -- I) {for (j = n; j> 0; -- j) {count + = 1 ;}} var t = new Date (); setTimeout (function () {console. log (new Date ()-t) ;}, 1000); heavyCompute (50000); Common setTimeout functions (or setInterval, such functions include fs. asynchronous APIs such as readFile .) When the latency is set to 1000 milliseconds, a parallel thread is created and then the code is executed immediately. However, the subsequent Code takes more time, so we waited for the subsequent code execution to complete and output the results, and then executed the original parallel thread, which would take more than one second. To verify whether the code in the parallel line is secretly executed in the background when node executes the code, I tested the following code: function heavyCompute (n) {var count = 0, I, j; for (I = n; I> 0; -- I) {for (j = n; j> 0; -- j) {count + = 1 ;}}} var t = new Date (); setTimeout (function () {heavyCompute (50000); // compare the difference between time by commenting on the sentence or not, we can see whether a parallel thread has a parallel execution console. log (new Date ()-t) ;}, 1000); var t1 = new Date (); heavyCompute (50000); console. log ('A'); console. log (new Date ()-t 1) The result shows that no one has moved the parallel process after it is created. To sum up, JavaScript is executed in a single thread. Even after the functions in the parallel thread are executed, the callback function can be executed only when the main thread is idle; we still return to the fact that JS is running in a single thread, which determines that JS cannot execute other code, including the callback function, before executing a piece of code. This conclusion is very important. In other words, a node can always execute only a piece of code at the same time. When it encounters a function such as settimeout, it immediately generates a parallel thread, then, place the parallel thread there and continue to execute the subsequent function. After the subsequent function execution is complete and the main thread is idle, return to execute the code in this parallel thread from the beginning, this is a specific node. js is unambiguous at all. <To be continued>