Home  >  Article  >  Web Front-end  >  Interesting explanation of callback functions in Node.js (with examples)

Interesting explanation of callback functions in Node.js (with examples)

青灯夜游
青灯夜游forward
2021-10-15 10:11:021521browse

This article will give you an interesting introduction to the callback function of Node.js, and give you a brief understanding of the callback function through examples. I hope it will be helpful to everyone!

Interesting explanation of callback functions in Node.js (with examples)

Interesting talk about the callback function of Node.js

The direct manifestation of Node.js asynchronous programming is the callback function. The callback function is in It will be called after completing the task, and Node.js uses a lot of callback functions. I think it is appropriate to use Node.js to talk about callback functions. Now let me try my best to talk about the callback function~ [Recommended study: "nodejs tutorial"]

What is the callback function

You go to an online forum to look for resource seeds, but the resource you are looking for cannot be found, so you post on the forum and leave your email address asking for resources. After a few days, a netizen finds the resource, so he sends you an email. Then you receive the resource seed and download the resource. Here, when you leave an email address in the forum, you register the callback function. The email address you leave is the callback function. When someone finds the resource and sends you an email, the callback function is triggered and the callback function is called. When you get the seed and download it, it is the response. callback event.

Example:

function main(info,callback){
    console.log("点赞、评论、转发了没?!")
    callback(info)
}

function say(msg){
    console.log(msg)
}

main("给了,给了!",say)

Here callback is the callback function. Of course, this name does not have to be used. In the function body, a message is output first, and then the callback function is called. The (callback) callback function uses msg as its parameter.

Callback function example

There are two ways to read files using Node.js program. One is a synchronous operation. Subsequent commands can only be executed after the read operation is completed. This method is called blocking. The other method is asynchronous, which allows you to read files while executing other commands. This method is also called non-blocking.

The non-blocking method is based on callback functions and allows parallel execution of operations. The operation result will be processed by the callback function when the event occurs, so the program can execute the next step without waiting for the result of an operation. This greatly improves the performance of Node.js and allows it to handle a large number of concurrent requests.

Example:

const fs = require("fs")

fs.readFile('./foo.txt',function(err,data){
    if(err) return console.error(err)
    console.log(data.toString())
})

console.log("Node.js 程序已经执行结束~")

Running result:

Node.js 程序已经执行结束~
小的们,快给我点赞~

It can be found that when reading a file, the following output statement will be executed regardless of whether the file is read. Therefore, the words that the program has ended will be displayed first, and then the file content will be displayed after waiting for the file to be read. The file content is returned as the parameter data of the callback function, so that you do not have to wait for the file I/O operation to complete before executing the code.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Interesting explanation of callback functions in Node.js (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete