A doubt about the case in the "callback" chapter of "JavaScript you don't know" (related to (setTimeout ())

topic description

Programs in

setTimeout () run ahead of time. Under normal circumstances, shouldn"t the setTimeout () function be executed after all execution has been completed in the current stack?

sources of topics and their own ideas

the case in the book "JavaScript you don"t know" is rewritten. In fact, I don"t have a thorough understanding of this case.

related codes

   

Apr.08,2021

execute asyncify (result) first, decompose the timer step, and you should be able to understand it.

var a = 0;
var timeoutFn = asyncify(result);
var b = setTimeout(timeoutFn,100);

aPP;
console.log('end');

because asyncify (result) is executed first.


you think
setTimeout (fn, 100)
fn is a defined function, which is executed after 100ms
but if you write it as setTimeout (fn (), 100) this is a function called by itself, the fn function will be executed immediately, and
will execute the function after fn return

after 100ms.
Menu