Analyzes the callback and code design modes in Node. js asynchronous programming, and node. js design modes
The biggest selling point of NodeJS is the event mechanism and asynchronous IO, which is not transparent to developers. Developers need to write code in asynchronous m
This article mainly introduces Node. callback and code design patterns in js asynchronous programming. Although long string parentheses during callback writing are not very nice in most cases, Node's asynchronous performance is indeed good, for more information, see the event mechanism and asynchronous IO, the biggest
node. JS callback functionThe immediate manifestation of node. JS asynchronous programming is callbacks.Asynchronous programming relies on callbacks to implement, but it cannot be said that the program is asynchronous after using the callback.The
This article focuses on introducing node. blocking and non-blocking calls of js callback functions involve node. knowledge about js callback functions. If you are interested in this article, learn it together. First,
of what is a, only when the function is called to specify the specific value of a, such as 2. Turn back, when calling doit, We need to specify what the callback is. As you can see, this function completes a sum function.The execution of the above code is:Call the doit function, the parameter is an anonymous function, enter the function body of doit, define A,B,C first, then execute the anonymous function just now, the parameter is a,b,c, return a T,
".Parse the HTTP server for node. JS: The first line requests (require) node. js's own HTTP module and assigns it to the HTTP variable. Next we invoke the function provided by the HTTP module: Createserver. This function returns an object that has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
This article mainly introduces a simple node. js server example. This article implements a simple helloworld example and shows how to run this server, for more information, see the following example: hello world.
It seems that the first section of each language tutorial w
. The callback function is called when the task is completed and node uses a large number of callback functions, and all node APIs support the callback function. For example, you can read the file while executing other commands, r
, the following is a lot of users mistakenly think of the callback:1/code Example 12 //the Foo function is intended to receive two parameters, any type A, and the function type CB, at the end to call CB ()3 functionFoo (A, CB) {4 Console.log (a);5 //Do something else6 //maybe get some parameters for CB7 varparam =math.random ();8 CB (param);9 }Ten //define a function called
the beginning of the queue, this result is returned to the user. This model is highly efficient and highly scalable because the web server keeps receiving requests without waiting for any read/write operations. (This is also called non-blocking IO or event-driven IO ). In the event-driven model, a main loop is generated to listen to events. When an event is detected, a callback function is triggered.
The entire event-driven process is implemented in
Because of the JavaScript language async feature. A callback function is used when you use node. js to run very many operations, including an Access database. Assume that the business logic in the code is slightly more complex, with a callback layer nested. So the code is very easy to enter
Label:Because of the JavaScript language async feature, callback functions are used when using node. js to perform many operations, including accessing the database. If the business logic in the code is a little more complicated, and the callback layer is nested, then the code is easy to get into
ObjectiveSince we are beginning to learn and learn about node. js, it is important to know some pre-knowledge points (actually knowledge about the operating system) before using it, including:
What is a callback function (callback);
What is synchronous/asynchronous;
What is I/O;
What is single thre
("End of program execution");Execute: The file is stored under the D:\softSetting\node\a folder, so: Execute code as followsHere is another example of a non-blocking read file:Create main2.js under the same D:\softSetting\node\a foldervar fs=require ("FS");Fs.readfile ("Input.txt", function (Err,data) {if (err) {retur
The immediate manifestation of node. JS asynchronous programming is callbacks.So what is a callback? A callback refers to passing a function as an argument to another function, and is usually called after the first function is complete. It is necessary to indicate that the callback
CALLBACK:A callback is A function so is passed as an argument to another function and is executed after its parent func tion has completed.node. JS callback functionThe immediate manifestation of node. JS asynchronous programming is callbacks.Asynchronous programming relies
Because of the asynchronous relationship, the code may be written in a different order than the order of execution, and may want to execute a before B, but because async might be executed before a. For example, using afnetworking to request data in OC and then refresh the page, because the network request is a block-implemented asynchronous method, So there is no data at the time of the refresh, in order to solve this problem, it will usually refresh
This article mainly introduces the elegant Processing Method of Nodejs asynchronous callback. This article uses the new features in ES6 and uses a very elegant method to solve the callback problem. For more information, see
Preface
The biggest highlight of Nodejs is its event-driven, non-blocking I/O model, which makes Nodejs highly concurrent processing capability and is very suitable for compiling networ
node. js is single-threaded, based on the event loop, non-blocking IO . An event queue is used in an event loop, and at each point in time , the system processes only one event , even if the computer has multiple CPU cores and cannot handle multiple events concurrently. as a result,node. JS is suitable for applications
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.