node. JS Learning Notes (2)--about async programming styles

Source: Internet
Author: User

The asynchronous programming style of node. JS is a big feature of it, which is reflected in the code in the callback.

The first is the sequential execution of the code:

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 ');

Here output 100000000 Hello, this description can only execute a function at the same 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 sequential execution of the thread, as long as a function set timeout, will immediately create a parallel line Cheng Lima return, and then let the JS main thread to execute the following code, after receiving the notification of this parallel thread, then execute the callback function. The result is HelloWorld rather than waiting for 1s to appear worldhello almost simultaneously.

2, the following scenario is a typical one:

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

In executing to the settimeout function (or setInterval These common, such functions also include asynchronous APIs such as those provided by Nodejs fs.readFile . ), see a 1000-millisecond delay setting, and then create a parallel path immediately after the execution of the code, but the subsequent code spent more time, so we wait for the following code to execute, output results, and then to perform the original parallel path, and this parallel path will take more than a second.

In order to verify that the code inside the parallel thread was secretly executed in the background when node executed the code behind it, 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);//through the comment or not, by comparing the difference between the time, we can see that the parallel path is not parallel execution
Console.log (New Date ()-T);
}, 1000);
var T1 = new Date ();
Heavycompute (50000);

Console.log (' a ');
Console.log (New Date ()-T1);

It turns out that no one has moved it since the parallel process was created.

Summing up, JS is a single-threaded execution, even if the parallel thread inside the function is completed, the callback function must wait until the main thread execution is idle to start execution;

We still go back to JS is a single-threaded run of this in fact, this determines that JS can not execute a piece of code before the execution of the code including the callback function.

This conclusion is very important, in other words, node at the same time can only execute a piece of code, encountered a function such as settimeout immediately generated a parallel path, and then put the parallel thread there to continue to execute the following function, after the completion of the function, the main thread is idle, Then come back to execute the code inside the parallel path from the beginning, this is a single-minded node. js, a bit unambiguous. < to Be continued >

node. JS Learning Notes (2)--about async programming styles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.