Understand the callback function of callback in javascript

  • 2020-03-30 03:50:29
  • OfStack

Recently, I've been looking at express, full of callbacks with functions as arguments. If this concept is not understood, the code for nodejs and express will be a mess. Such as:

app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;

App is an object, use is a method, the parameter of the method is an anonymous function with parameters, the function body is directly given in the following. How does this code make sense? Let's start with the concept of a callback function.
The first thing to understand is that in js, functions are also objects, can be assigned to variables, and can be placed as arguments in a function's argument list. Such as:

var doSomething = function(a,b)
 return a + b;

What this code means is that it defines an anonymous function that is just like any other function except that it has no name. The anonymous function is then assigned to the variable doSomething. Next we call:


This will print 5.

A callback function is placed in the argument list of another function (such as parent), passed to the parent as an argument, and then executed somewhere in the parent body. Abstract, look at the example:

// To illustrate the concept of callback
var doit = function(callback)
    var a = 1,
        b = 2,
        c = 3;
    var t = callback(a,b,c);
    return t + 10;
var d = doit(function(x,y,z){
    return (x+y+z);

Define the doit function first, with a parameter callback. This callback is the callback function and can be arbitrarily named. If you look at the body of the function, you define the three variables a,b, and c. Then the callback function is called. Finally, a value is returned.

The next step is to call the doit function. Note that the callback was not defined when doit was defined, so you didn't know what the callback was for. It is easy to understand, we usually define functions, parameters is given a name, such as a, the use of a in the body of the function, but also do not know what a throughout the whole process, only when call the function to specify a specific value, such as 2. Looking back, at the time of call doit, what we need to specify the callback is a thing. As you can see, this function performs a sum function.

The execution process of the above code is:

Call the doit function, parameter is an anonymous function; Enter the body of the doit function, define a,b,c, then execute the anonymous function, a,b,c, and return a t, and finally return a t+10 to d.

Back to the original example, app.use(...) It's a function call. We can imagine that a use method must have been defined before, but it is not given here. When these two examples are compared, we can understand them immediately.

When using nodejs, express, it is impossible to find the function definition for every method or function. So just know what parameters are passed to the callback in that definition. Then when we call a method or function, we define our own anonymous function in the argument to do something.

The Over!

Related articles: