node. JS callback function callback (reprint), said very clearly, read it again understand

Source: Internet
Author: User
Tags what callback

Recently, looking at Express, eyeful, everywhere is the use of callback functions as parameters of the function. If this concept is not understood, Nodejs, express code will see a mess. Like what:

Copy CodeThe code is as follows:
App.use (function (req, res, next) {
var err = new Error (' not Found ');
Err.status = 404;
Next (ERR);
});


The app is an object, use is a method, the parameter of the method is an anonymous function with parameters, and the function body is given directly at the back. How does this code understand? Let's begin by understanding the concept of callback functions.
First of all, in JS, the function is also an object, you can assign a value to the variable, can be placed as a parameter in the function's argument list. Like what:

Copy CodeThe code is as follows:
var dosomething = function (A, B)
{
return a + B;
}


The meaning of this code is to define an anonymous function, which, in addition to having no name, is nothing like a normal function. The anonymous function is then assigned a value to the variable dosomething. Next we call:

Copy CodeThe code is as follows:
Console.log (dosomething (2,3));

This will output 5.

The callback function, which is placed in a parameter list of another function (such as parent), is passed as a parameter to the parent and then executed at a location in the parent function body. To be abstract, look at an example:

Copy CodeThe code is as follows:
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);
});
Console.log (d);


Define the doit function first and have a parameter callback. This callback is the callback function, the name can be arbitrarily taken. Look at the function body, first define three variables A,b,c. Then call the callback function. Finally, a value is returned.

The doit function is called below. Note that the definition of doit just now, callback is not defined, so just did not know what callback is for. This is actually very good understanding, we usually define the function, the parameters are only given a name, such as a, in the function body to use a, but the whole process is not aware of what is a, only when the function is called to specify the specific value of a, such as 2. Turn back, when calling doit, We need to specify what the callback is. As you can see, this function completes a sum function.

The execution of the above code is:

Call the doit function, the parameter is an anonymous function, enter the function body of doit, define A,B,C first, then execute the anonymous function just now, the parameter is a,b,c, return a T, and finally return a t+10 to D.

Back to the original example, App.use (...) is a function call. We can imagine that a use method must have been defined before, but not given here. A comparison of these two examples can be understood immediately.

In the use of Nodejs, Express, it is not possible for each method or function we have to find its function definition to look at. So just know what parameters are passed to the callback in that definition. Then, when invoking a method or function, we define the anonymous function in the parameter to accomplish some function.

node. JS callback function callback (reprint), said very clearly, read it again understand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.