Previously written asynchronous JS is a pure translation, and now just work for 2 months, want to talk about my own JS callback function of a little understanding, welcome the desire for everyone to correct and exchange.
The callback function from the formal view is the function B as a parameter to function A, in a function body called function b
1 function A (b) {2 B (); 3 }4function B () {5 ... 6 }
What is the point of doing this?
1. Control the execution process, function B only executes after function a executes
2. Depending on the requirements, there can be a variety of function B
3. Most importantly, it is possible to pass the data obtained through function A to function b
1 function A (callback) { 2 ... 3 var record; // After a series of complicated operations, I got the data record 4 callback (record); 5 6 function b (data) { 7 ... // data to dispose of 8 9 A (b);
We are interested in the record in function A and want to do a variety of operations on the record elsewhere in the program, then we can pass in the corresponding callback function. Of course, one of the obvious drawbacks of this approach (my current understanding) is that if we are interested in record_2, then we are not flexible enough.
A little understanding of JS callback function