NodeJS study notes-callback function and callback hell

What is a callback function

Programming is divided into two categories: system programming and application programming. The so-called system programming , in simple terms, is to write a library; and application programming is to use a variety of libraries to write a program with a certain function, that is, an application. System programmers will leave some interfaces for libraries written by themselves, that is, API (application programming interface, application programming interface) for application programmers to use. So in the abstraction layer diagram, the library is located under the application.

When the program runs, under normal circumstances, the application will often call the pre-prepared functions in the library through the API. However, some library functions (that is, intermediate functions ) require the application to pass a function to it first, so that it can be called at the right time to complete the target task. The function that is passed in and then called is called the callback function . The intermediate function and the callback function are two necessary parts of the callback, but people often overlook the third important part of the callback, which is the caller of the intermediate function. In most cases, this caller can be equated with the main function of the program, but to show the difference, I call it the start function here .

Link: https://www.zhihu.com/question/19801131/answer/27459821
Source: Zhihu

Simply put, the callback function can be interpreted as "If you call me, I will call you back." The initial function calls the intermediate function, and the intermediate function needs to call the callback function passed in as a parameter.

Since the nodejs program is asynchronous, if you want to perform blocking programming sequence execution, you can use the callback function to achieve. Generally, IO operations  such as network communication, reading and writing files, and interacting with the database  will use callback functions, and it takes some time to return results.


Callback function in Nodejs

The simplest callback function is shown below, where x is called by the intermediate function y as a callback function

let x=function(){
    console.log("i am called from inside a function")
};

let y=function(callback){
    console.log('do something');
    callback();
}

y(x);

Look at another example:

// getTodos函数定义
const getTodos = function(callback){
    setTimeout(function(){ //调用应用层的setTimeout函数,并传入callback函数
        console.log('运行了3s');
        console.log('2');
        callback('回调函数');
        console.log('运行完毕');
    },3000);
};

console.log('1')
// 执行getTodos函数,并传入callback函数
getTodos(function(data){
    console.log(data);
});
console.log('3')

After the program runs, the output result is:

1
3
运行了3s
2
回调函数
运行完毕

The desired result is: 1 => run 3s => 2 => 3

Therefore, you need to put the following function in the callback function and modify it to:

// getTodos函数定义
const getTodos = function(callback){
    setTimeout(function(){ //调用应用层的setTimeout函数,并传入callback函数
        console.log('运行了3s');
        console.log('2');
        callback('回调函数');
        console.log('运行完毕');
    },3000);
};

console.log('1')
// 执行getTodos函数,并传入callback函数
getTodos(function(data){
    console.log(data);
    console.log('3') // 放进回调函数中
});

The output after running is:

1
运行了3s
2
回调函数
3
运行完毕

Summary: Execute the callback function Role: realize blocking programming, execute the original program and the callback function in order


Callback hell

When writing a js program, sometimes it is necessary to nest several callback functions, which not only looks bloated, but also greatly increases the difficulty of code maintenance, which is called "Callback hell". As shown in the figure below, it is a classic  Callback hell :

 

Guess you like

Origin blog.csdn.net/qq_14997473/article/details/109338095