Event loop micro task macro task

First, JavaScript is a single-threaded scripting language.
So that is to say, during the execution of one line of code, there must be no other line of code executed at the same time, just like alert()going crazy after using console.logit. If the pop-up box is not closed, the console will not display a logmessage.
Or some codes perform a lot of calculations, such as ghost operations such as brute force password cracking on the front end, which will cause the subsequent codes to wait all the time, and the page is in a state of suspended animation, because the previous codes have not been executed.

1. Micro-tasks, macro-tasks

Macro task (macrotask)

  • Asynchronous Ajax requests
  • setTimeout、setInterval
  • file operation
  • Other macro tasks

microtask

  • Promise.then、.catch 和 .finally
  • process.nextTick
  • other microtasks

Both tasks are asynchronous tasks, the difference is the order of execution. The message queue  has a micro first and a micro, and the micro can be plugged into the macro queue

2. Execute the specific sequence of steps 

Execute the synchronous code first, and put the asynchronous macro task into the macro task queue when
encountering the asynchronous macro task, and put the asynchronous micro task into the micro task queue
when encountering the asynchronous micro task. Transferred from the queue to the main thread for execution
After the microtask is executed, the asynchronous macrotask is transferred from the queue to the main thread for execution, and the cycle continues until all tasks are executed.

3. Interview questions: Please analyze the order of the following code output (the code is longer, and it has been intercepted into 3 parts from the left, middle and right)

The correct output sequence is: 156234789  

Guess you like

Origin blog.csdn.net/qq_37485170/article/details/130111693