concept

  • Javascript single-threaded execution, in javascript asynchronous implementation is implemented through event loops (similar to early Windows programming).

  • Macro task

A queue, macro tasks are parallel to the current executing script, such as setTimeOut, etc

  • Micro tasks

A queue. A microtask is a macro task, such as a promise, that waits for execution after the current script is executed

  • Event loop

Event loop logic

while (EventLoop.waitForTask()) {
  const taskQueue = EventLoop.selectTaskQueue();
  if (taskQueue.hasNextTask()) {
    taskQueue.processNextTask();
  }

  const microtaskQueue = EventLoop.microTaskQueue;
  while (microtaskQueue.hasNextMicrotask()) {
    microtaskQueue.processNextMicrotask();
  }

  rerender();
}
Copy the code