Easy Tutorial
❮ Programmer Joke 21 Eclipse Shortcuts ❯

A Thorough Understanding of Callback Functions in Node.js

Category Programming Techniques

What exactly is a callback function? There are countless articles online, most of which are confusing to read. These articles can be roughly divided into two categories: the first category is filled with too many technical terms, making it unreadable without understanding the terms; the second category, on the other hand, does not explain the terms and only uses some life examples that are detached from programming to make analogies, which makes readers even more confused.

As the core of JS, callback functions are closely related to asynchronous execution. Without crossing this threshold, many callback codes can be dizzying!

>

Citing a description from a Stack Overflow expert, in fact, a callback is very simple and pure:

A "callback" is any function that is called by another function which takes the first function as a parameter. (Calling one function within another function is a callback)

Here is the simplest example:

function a() {
    return 1
}

function b(aa) {
    return 2 + aa
}

// Call:
var c=0
c = b(a()) // A is a function, but it is also called as a parameter in function B
console.log(c) // The result shows 3

The above example is very easy to understand. Now let's introduce another concept: asynchronous

Look at the following code:

var a = 0

function bb(x) {
    console.log(x)
}

function timer(time) {
    setTimeout(function () {
        a=6
    }, time);
}

// Call:
console.log(a)
timer(3000)
bb(a)

The above code is very simple. The logic we need is that the global variable a is initially 0, and then after 3 seconds, it becomes 6, and then it is printed out. It seems that there is no problem with the above code, and it theoretically meets our logical needs, but the result is as follows:

0
0

What's going on?

Because JS is an asynchronous execution language, even though the timer function sets a=6, JS will not wait for the time to end before exiting the function, but will immediately execute the next statement (i.e., call the bb function). At this time, the 3 seconds have not ended, and a has not been reassigned, so it is still printed as 0.

The callback function can solve this problem:

var a = 0

function bb(x) {
    console.log(x)
}

function timer(time, callback) {
    setTimeout(function () {
        a = 6
        callback(a);
    }, time);
}

// Call:
console.log(a)
timer(3000,bb)

This time, a keyword callback was added in the timer function, which means that it is not an ordinary parameter here, but a function name. Pay attention, the key point is coming:

>

Generally speaking, the formal parameters of a function refer to the entry for passing variables from the outside to the inside of the function body, but after adding the callback here, it is completely the opposite. It refers to the exit for calling the external function after the function body has completed a certain mission! At this time, you should understand what is called "callback", which means calling the external function back.

In this example, after 3 seconds, first a=6, and then the function bb(x) is called through the keyword callback(a), and the result is:

0
6

This logic meets our needs.

In terms of writing, you don't need to define the function bb. You can directly write it in the form of a function when calling timer, and change the call part like this, which is completely the same:

console.log(a)
timer(3000, function (x) {
    console.log(x)
})

This way of writing does not even need a function name (technically called an "anonymous function"), which is more common and easier to understand in Node.js code, translating into natural language means: set a timer for 3 seconds, and then call back the content inside function(x) after completion.

Node.js programming uses a lot of asynchronous programming techniques to make efficient use of hardware, and at the same time, it does not cause synchronous blocking. In fact, Node.js still implements asynchronous operations through multi-threading technology at the bottom, but ordinary users do not need to delve into its implementation method. We just need to do our asynchronous processing.

>

Author: rockage

Original article: https://blog.csdn.net/rockage/article/details/79513450

**Click to share notes

Cancel

-

-

❮ Programmer Joke 21 Eclipse Shortcuts ❯