node. JS callback function, event loop, Eventemitter ar

Source: Internet
Author: User

Asynchronous programming

The immediate manifestation of node. JS programming is callbacks, and asynchronous programming relies on callbacks: node uses a number of callback functions, all of which support callback functions. such as reading files.

Here are the three nouns that do not understand threading and asynchronous and synchronous slightly.

Threading: Simply saying is doing one thing. That is, execute a program code. The JS engine is a single-threaded process. A single thread is saying that only one thing can be done at a time.

Synchronization: This is the thing that must be done before the first to do the back, or the latter can not be completed. It's like our JS code will run from the top down.

Async: Simply said is two threads, you do your, I do mine, mutual does not affect.

Concurrency: That means that multiple asynchronous threads are running at the same time, so this is the most efficient way to handle the situation.

In the case of synchronization, this must go from top to bottom, and of course it could be two threads, but these two threads must be done before the next one can be executed.

such as: function Fun () {

var x=1; Run this line first

var y=1;//run this line again

Consoel.log (x+y);//Last Run this line

}

Fun (); Call the Fun function

In the case of asynchrony, everyone does the work, not to mention who is finished first.

such as: Console.log ("12221");

SetTimeout (function () {

Console.log ("111");

},1000);

Console.log ("13331");

This timer, settimeout, will open an asynchronous thread, wait a second before executing the code in the callback function. The output of block 13331 is not executed.

There is, of course, an asynchronous concurrency security problem, which means that when two asynchronous threads do one thing at a time, it creates an asynchronous concurrency security issue.

Node callback function

Okay, here we go. Node's callback function.

The following is an example of blocking code and non-blocking code:

Block code: VAR fs=require ("FS"); Get FS module as file read module

var data=fs.readfilesync ("Input.txt");//Read Input.txt file content is the Novice Tutorial official website address:www. Runoob. COM

Console.log (Data.tostring ());

Console.log ("End of program execution");

Execute: The file is stored under the D:\softSetting\node\a folder, so: Execute code as follows

Here is another example of a non-blocking read file:

Create main2.js under the same D:\softSetting\node\a folder

var fs=require ("FS");

Fs.readfile ("Input.txt", function (Err,data) {

if (err) {return console.error (err);}

Console.log (Data.tostring ());

});

Console.log ("End of program execution");

Perform:

  

The callback function for node is this: function (err,data) {}

Err indicates an error, and the Err content is automatically present when the callback function performs an error, and data indicates that the callback function receives it.

Node Event loop-This is more difficult

node. JS is a single-process single-threaded application, but supports concurrency through events and callbacks, so performance is very high

Each of the APIs for node. JS is asynchronous and runs as a standalone thread, using an asynchronous function call, and handling concurrency.

node. js basically all of the event mechanisms are implemented using the Observer pattern in design mode . This next chapter is a separate story.

The node. JS single thread resembles an event loop that enters a while (true) until no event observer exits, and each asynchronous event generates an event watcher that invokes the callback function if an event occurs.

The above is the basic model of the Node event loop, also known as non-blocking IO or event-driven IO, webserver has been accepting requests without waiting for any read-write operations, Eventemitter binding events on and listening for events (event).

In the event-driven model, a main loop event loop is generated that triggers a callback function event handlers when an event is detected.

eventemitter Supplement:

             

node. JS callback function, event loop, Eventemitter ar

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.