After reading dozens of gaudy articles, I still don't know whether the callback in javascript has anything to do with asynchronism.

is an example of writing n parameters and n heap execution statements that you don"t want to take from other people"s articles.
I have written three simple examples myself, which contain my understanding and questions about callback functions. I read teacher Ruan"s article, too, open the door and say that callback is a solution to asynchronous programming. But I still don"t understand the relationship.

//3
function f2() {
    setTimeout(()=>{
     console.log("f2 finished") 
    },0)    
}

function f1(cb) {
    cb(); 
    console.log("f1 finished")
}

f1(f2);

:
//

first of all, the callback function is actually a common function, which has nothing to do with asynchrony;

secondly, why callbacks always appear with asynchronous mixers, mainly because callback functions are commonly used to receive feedback on asynchronous operations (including successes and errors);

take an example:
the PE teacher (main thread) gave the student a call (return function) and asked the student to run 10 laps (asynchronous operation), agreed with the student to call him again after the run (finished), and directly called the infirmary if he was hurt (making a mistake) while running. Then the student began to run the circle, and the teacher went to do something else.

if the asynchronous operation does not require feedback, it does not need a callback function;
//,  ,, .
var callBack = function(err,val){
    if(err) return console.log("!");
    console.log("!");
    if(val) console.log(":",val);
}

//, ,.
function syncFn(a,b,cb){
    setTimeout(function(){
        try{
            var v = a + b; 
            cb(null,v);
        }catch(e){
            cb(e)
        }
    },100)
}

doesn't understand what these three examples are trying to express. First, the first example has nothing to do with asynchronism, but it just passes the function as a parameter and has something to do with the call stack; the second example, setTimeout, is an asynchronous method that puts the passed function in the queue as a callback function; the third example is the same.


doesn't necessarily matter, but callbacks are a common way to handle asynchronism


setTime, whether it is zero or anything, is put in another queue, which is the behavior of the browser. The asynchronous and non-asynchronous js execution mechanism is specified, and the browser itself does so

. The

callback is to ensure that the next step can be performed when a certain condition is met


upstairs is quite right. The callback function is a way for asynchronous API to feedback back to the main thread, not to implement it asynchronously. A typical implementation of asynchrony is collaborative programming.


I'll try to answer that, too.

after looking at so many answers upstairs and your questions, I think what puzzles you is that you don't understand the difference between "asynchronous functions" and ordinary "Synchronize functions".

in fact, the main difference between them is "event loop queue". If you hear the word for the first time, you may be scared for a moment, if it is not the first time you have heard of it. Then you may not really understand it and its relationship with asynchronous functions.

is actually quite simple. The event loop queue is just a "to-do list". Note that it is "to-do", not "do it right away". Async is added to the to-do list, and Synchronize is not added to the to-do list. That's the essential difference.

The

event loop queue is not directly open to JS code, so we cannot manipulate it directly, only indirectly, mainly in the following ways:

  1. built-in timer functions (setTimeout/clearTimeout, setInterval/clearInterval)
  2. Ajax request for asynchronous mode
  3. newly created Promise object
  4. async function (essentially Promise)

all four of the above are asynchronous, but all the other code is Synchronize (unless js adds a new asynchronous method later).

take a timer, for example, which puts a function in the event queue until it is executed at some point in the future. It is the same even if the passed-in time is 0, so the following code prints 2 first and then 1:

setTimeout(() => console.log(1), 0);
console.log(2);

so summarize:

  1. the key to the difference between asynchronous and Synchronize functions is the event loop queue
  2. it is not the use of the callback function that makes a function asynchronous. The Synchronize function can also use callbacks
  3. js there are only a few fixed ways to create asynchronous functions, except for Synchronize
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9671b-15df.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9671b-15df.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?