Foreword
In my image, asynchrony first appeared with ajax. I was still working on .net at that time, and then .net actually came out with an asynchronous control...
Although I finally found out that it is not asynchronous... Then, the front-end is used a lot of asynchronous. If it is not an asynchronous program, you would be embarrassed to say that the NodeJs you wrote was made by JavaScript.
The feature of asynchronous programming model has also been brought over. Asynchronous has many advantages, but it is a nightmare for design. Asynchronous will disrupt the timing, so it increases the difficulty of design,
But asynchrony has revolutionized performance improvement and user experience, so the asynchronous features of NodeJS are quite obvious. Today we will learn briefly
Asynchronous I/O
In fact, at the operating system level, there are only two I/O methods, blocking and non-blocking
In the blocking model, the application needs to wait for the I/O to be completed before returning the result. Its characteristic is that it calls the backend and waits for the system to complete all operations. This will cause the CPU to wait, while non-blocking calls will return immediately.
I first read a book, but I feel that the description here is not clear, and the asynchronous model is actually very big, just an improvement in the level of perception. Let me give a simple example
I now have two single-page application views, a search page and a list page. When I search, I need to search through various channels. Shenzhen needs to call a third party, and the third party obtains data from specific channels
Of course it is very slow at this time. If I switch directly from A to B and then create a loading box or something to load data in B, there will be no problem. But the problem now is that I need animation effects to switch from A to B
This requires that Bview rendering has ended when switching, at least it will not get data in the process and start rendering, so asynchronous may not be so easy to use at this time. Even if it requests data asynchronously, it also requires data acquisition to load the page.
This is still blocking loading, and there is no way to do this in terms of business
No technology is perfect. Blocking causes a waste of CPU waiting. Not only does non-blocking disrupt logic, it may also require polling to confirm whether the loading is completed (I once used polling to detect whether a DOM was generated)
NodeJs uses an event loop mechanism. When the process starts, Node will create an infinite loop. Each time the loop body is executed, the process is a Tick. The process of each Tick is to see if there are events that need to be processed
If there is, take out the relevant event, execute it, and then enter the next logic. If not, exit the loop
In each Tick process, there are one or more observers in each event loop. The process of determining whether there is an event to be processed is to ask these observers whether the event needs to be processed
Take our html event model as an example
For HTML, in fact, each of its DOM is an observer. The DOM of the page observes the changes of our Web Page. After we provide an addEventListener to a DOM, we will register a callback function for it. The events we register will be put into a "container" object. At this time, it is just registration. These functions will be triggered after the conditions are met (when the page changes), and the relevant events will be taken out of the container and executed
We now click on a point on the page once, and then we will take out the click event collection from the container, we will find the relevant DOM, and then trigger the callback functions of these DOM
Events may come from user clicks or data changes. In Node, events mainly come from network requests and file I/O. These events will have corresponding observers, such as file observers and network observers
This is also a typical production/consumption model, asynchronous I/O, network requests provide event production, events are delivered to various observers, observers register events, and the event loop is responsible for taking out events and then executing them
PS: Taking click as an example, each DOM observer first registers the event, the page process continuously monitors the page, the user clicks the page to generate events, and then the registered click event is taken out from the container and executed,
General function logic is controlled by us:
var forEach = function (list, callback) {
for (var i = 0, len = list.length; i callback(list[i], i, list);
}
}
In the case of asynchronous operation, the callback function is not controlled by the developer. Each time js is called, a transition product request object will be generated
fs.open = function (path, flags, mode, callback) {
bingding.open(pathModule._makeLong(path), stringToFlags(flags), mode, callback);
};
fs.open opens a file based on the path and parameters to get relevant data. It calls the c related interface internally, and an intermediate object will be generated in the process, in which all our states will be...
PS: I feel bad after watching it for so long
Conclusion
The above is all about asynchronous I/O in nodejs. Personal summary. If there are any omissions or errors, please point them out.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
