The case where the setTimeout callback function executes the function immediately

if ordinary functions are put in setTimeout, which is a normal event loop mechanism, the code in setTimeout will be executed after the code in the main thread has been executed, as follows:

  console.log("start")
  setTimeout(function(){
    console.log("setTimeout")
  },0)
  console.log("end")

output result:

clipboard.png

:


:

clipboard.png
the question I want to ask is that if the immediate execution function is used, will the setTimeout not be placed in the asynchronous event queue, or will it run the function in the asynchronous event queue and then go back to the main thread because the function is executed immediately? Thank you for the answer.

Dec.15,2021

now that you know that executes the function immediately, " execute " literally, it doesn't have to wait for a callback, and the code loads and executes immediately.
in your second way, the parameter received by setTimeout is actually the return value of your immediate execution function , that is, undefined

console.log('start');
setTimeout((function () {
  console.log('');
  return function () {
    console.log('setTimeout');
  };
})(), 0);
console.log('end');

you can try to run this section. Give the immediate execution function return a function , and setTimeout executes this function

.

Brother, the timeout call is asynchronous even if you write 0, and it will be executed last as long as it is asynchronous.


non-functions perform operations such as eval and new Function;
, however, is not the return value of the function, but just an immediate execution function

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-1b36f56-4ecbd.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-1b36f56-4ecbd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?