Objective
Since we are beginning to learn and learn about node. js, it is important to know some pre-knowledge points (actually knowledge about the operating system) before using it, including:
- What is a callback function (callback);
- What is synchronous/asynchronous;
- What is I/O;
- What is single thread/multithreading;
- What is blocking/non-blocking;
- What is event and event driven;
- What is an event-driven callback;
- What is the event loop;
This article is the first of a series of articles to introduce callback functions. (See Reference at the end of the article)
What is a callback function (callback)
I think understanding a concept should be from simple to complex, from application to principle, from phenomenon to essence, in short, from shallow into deep.
So, let's start with the surface, the callback function, can be first superficial understanding:
A "callback" is an any function which is called by another function which takes the first function as a parameter.
That is, calling another function in one function is callback
As the following simple code, is an example of a callback function:
function callback() { alert("I am in the callback!");}function work(func) { alert("I am calling the callback!"); func(); }work(callback);
In the above example, the callback function is passed as a variable into the function work;
and is called in the work.
So conclusion 1:
The callback function is the function A that can be used as a variable--passed to other function B calls;
Two process & application of callback function
As we know from the above, the callback function is actually a function variable passed into the other principal functions.
Knowing the concept, let's look at the workflow of the callback function:
?? S1 creates a function A, which can be used to do addition;
?? S2 creates a delay function B, which calculates the length of time (practically all the API implementations provided by the library function);
?? S3 we pass a into B, as a parameter, this step is called the registration callback function ;
?? S4 in B calls the execution force passed in function A, this step is called call callback function ;
We have created the callback function A, which is the function of:
?? After 10s, returns a result of performing an addition operation;
To implement this function is very simple, so why should we introduce a callback function?
This has to refer to the role of abstraction.
In the above example, the function of a is to perform an addition operation, but what if we want to do multiplication, division, sorting, and so on? The role of B is delay, but what if you want to respond to other events such as clicks, commits, etc.?
Therefore, the callback function is introduced: it is for us to be able to perform the more extensive and flexible function requirements according to the function of the function A, and to keep the code clean and maintained.
The above is the understanding of the callback function. (In fact, it also involves the concept of events)
Finally, a more popular explanation to understand:
You go to a shop to buy things, just what you want to have no goods, so you left your phone in the shop clerk, after a few days the store has goods, the clerk called you, and then you received a call to the store to pick up the goods.
In this example, your phone number is called a callback function;
You leave the phone to the clerk, it's called the registration callback function,
The store was later in stock. The event that triggered the callback association.
The clerk called you called the callback function.
You go to the store to pick up the goods called the response callback event.
Three refernce:
?? Question: What is a callback function
node. js Pre-Knowledge point (a): callback function