Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Node.js callback function

The direct embodiment of node.js asynchronous programming is the callback function, which will be called after completing the task, and Node.js uses a large number of callback functions, I think it is appropriate to use Node.js to talk about callback functions. Now I’m going to try to make the callback function interesting

What is a callback function

You go to the online forum to find the seed of resources, but the resources you want to find can not be found, so you post in the forum to leave a mailbox for resources, a few days later, some net friends find resources, so send an email to you, and then you received the seed of resources and then go to download resources. In this case, if you leave an email in the forum you register the callback function, the email you leave is the callback function, someone finds a resource and sends you an email that triggers the callback function and calls the callback function, you get the seed and you download it and you respond to the callback event.

Example:

function main(info,callback){
    console.log("Liked, commented, retweeted? !")
    callback(info)
}
​
function say(msg){
    console.log(msg)
}
​
main("Yes, yes!,say)
Copy the code

In this case, callback is the callback function, but it doesn’t have to be called that. In the body of the function, a message is printed and then the callback function is called, which takes MSG as its argument.

Example callback function

There are two ways to read files using the Node.js program. One is a synchronous operation in which subsequent commands cannot proceed until the read operation is complete. This method is called blocking. The other is asynchronous, where you can read files while executing other commands, which is also called non-blocking.

The non-blocking mode is based on the callback function, allowing parallel execution of operations. The result of an operation is 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, which greatly improves the performance of Node.js to handle large numbers of concurrent requests.

For 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 program has finished executing ~")
Copy the code

Running results:

Node.js program has finished executing ~ small people, give me a thumbs up ~Copy the code

As you can see, when a file is read, the following output statement is executed, whether the file is read or not. Therefore, the program will first display the end of the speech, and then wait for the file to finish reading and then display the contents of the file. The contents of the file are returned as data, so that you don’t have to wait for the file I/O operation to complete before executing the code.