Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning of callback in nodejs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the meaning of callback in nodejs". In daily operation, I believe that many people have doubts about the meaning of callback in nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is the meaning of callback in nodejs?" Next, please follow the editor to study!

In nodejs, callbacks pass one function as an argument to another and are usually called after the first function completes; it is an asynchronous equivalent, and all API in Node supports callbacks.

This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.

What does callback mean in nodejs

Callbacks are asynchronous equivalents. The callback function is called after completing a specific task. Node makes heavy use of callbacks. All of Node's API supports callbacks.

For example, a function reading a file may start reading the file and allow the next instruction to be executed immediately back to control to the execution environment. Once the file Iswap O is complete, it invokes the callback function and passes the callback function, with the contents of the file as parameters. Therefore, there is no blocking or waiting for file I / O. This makes Node.js highly scalable because it can handle a large number of requests without waiting for any function to return the result.

Blocking code example

Create a file named input.txt with the following text

Yiibai Point is giving self learning contentto teach the world in simple and easy way!

Create a js file called main.js with the following code:

Var fs = require ("fs"); var data = fs.readFileSync ('input.txt'); console.log (data.toString ()); console.log ("Program Ended")

Now run main.js to see the result:

$node main.js

Verify output

Yiibai Point is giving self learning contentto teach the world in simple and easy way!Program Ended

Non-blocking code example

Create a file named input.txt with the following text

Yiibai Point is giving self learning contentto teach the world in simple and easy way!

Update the main.js file like the following code:

Var fs = require ("fs"); fs.readFile ('input.txt', function (err, data) {if (err) return console.error (err); console.log (data.toString ());}); console.log ("Program Ended")

Now run main.js to see the result:

$node test.js

Verify output

Program EndedYiibai Point is giving self learning contentto teach the world in simple and easy way!

These two examples illustrate the concepts of blocking and non-blocking calls. The first example shows the block until it reads the file and then proceeds only to the end of the program. In the second example, the program does not wait for the file to be read, but it simply prints "Program Ended" while the program continues to read the file without blocking.

Therefore, the blocking program executes in the sequence and looks at its easier logic from the programming point, but the non-blocking scheme is not executed sequentially, in which case any data that a program needs to use is processed, and it should keep the same blocks in use. make it execute sequentially.

At this point, the study of "what is the meaning of callback in nodejs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report