js-visualizer-9000-client icon indicating copy to clipboard operation
js-visualizer-9000-client copied to clipboard

Micro task vs Macro task

Open dhanush-kannan-dk opened this issue 1 year ago • 1 comments

As per the flow, I can see Micro task is executed before the macro task. So the Event loop pseudocode may be:

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

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

Example code:

setTimeout(function a() {console.log("Macro");}, 0);

Promise.resolve().then(function b(){console.log("Micro");});

console.log("Stack");

Output: Stack Micro Macro

dhanush-kannan-dk avatar Jul 02 '24 00:07 dhanush-kannan-dk