1. What is a callback function?

Callback function A function called through a pointer. If you pass this pointer as an argument to another function, when the function to which the pointer points is called, we say that this function is a callback function. A callback function is not called directly by the implementer of the function, but is called by another function at a specific time or condition.

The function of the callback function

To distinguish the caller from the called, the caller does not care who the called is, it only needs to know that there is a called function with a particular stereotype and constraints. In short, callbacks allow the user to pass the function that needs to be called as a pointer to another function, making the program more flexible.

3. Disadvantages of callback functions: callback hell

The nature of callback addresses: ① Nested functions have serious coupling, which affects the whole body. (2) Error handling is difficult, can not use try catch and direct return.

4. How to solve callback hell?

Three principles
  • Keep the code shallow
  • modular
  • Handle every error

implementation

  • Disassemble functions. Disassemble each part into a function.
  • Event publishing/listening mode
On the one hand, it listens for events to occur and executes the corresponding callback if they occur, and on the other hand, it listens for the completion of an operation and executes the corresponding callback when the operation is complete.Copy the code
  • Promise
readFile('./sample.txt').then(content => {
    let keyword = content.substring(0, 5);
    return queryDB(keyword);
}).then(res => {
    return getData(res.length);
}).then(data => {
    console.log(data);
}).catch(err => {
    console.warn(err);
});
Copy the code
  • generator
// Our main task -- display the keyword // use yield to interrupt the execution of the code below // Yield is followed by the promise object const showKeyword =function* (filepath) {
    console.log('Start reading');
    let keyword = yield readFile(filepath); Console. log(' the keyword is${filepath}`); } // Flow control of the generatorlet gen = showKeyword();
let res = gen.next();
res.value.then(res => gen.next(res));
Copy the code
  • async/await
As you can see, all of the above methods solve some of the problems of callbacks in asynchronous programming.
  • The way functions are split is simply to split blocks of code, often to the detriment of subsequent maintenance;
  • The event publishing/listening mode fuzzy the process relationship between asynchronous methods;
  • Although Promise enables multiple nested asynchronous calls to operate through chained apis, excessive THEN also increases the redundancy of the code and interferes with the asynchronous tasks in each stage of reading the code.
  • The over-generator provides a good syntactic structure, but the context of generator and yield is somewhat inappropriate here.
So here’s another method, which is async/await in ES7.

A brief introduction to async/await. Basically, any function can be an async function, and the following are legal:

const printData = async function (filepath) {
   let keyword = await readFile(filepath);
   let count = await queryDB(keyword);
   let data = await getData(res.length);
   console.log(data);
});
printData('./sample.txt');
Copy the code