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

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

青灯夜游
Release: 2021-10-15 10:11:02
forward
1526 people have browsed it

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)
Copy after login

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 程序已经执行结束~")
Copy after login

Running result:

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

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template