2

I guess that the microTask is processed in any steps.

And I tested these things, but I'm not sure that my assumption is right.

Test 1 - macroTask & microTask

for (let i = 0; i < 2; i += 1) {
  setTimeout(() => {
    console.log('-- macro --')
    queueMicrotask(() => console.log('** micro **'));
  });
}

results

-- macro --
** micro **
-- macro --
** micro **

Test 2 - microTask & animation frame

for (let i = 0; i < 3; i += 1) {
  requestAnimationFrame(() => {
    console.log('@@ animation callback @@');
    queueMicrotask(() => {
      console.log('** micro **');
    });
  });
}

results

@@ animation callback @@
** micro **
@@ animation callback @@
** micro **
@@ animation callback @@
** micro **

I was surprised when I first saw the results of this test. Because I thought that all tasks in the animation frame queue will be processed at once.

But, now I think that "while processing the animation frame callback functions, check if the microTaskQueue is empty, and if there is a task inside, process it, and call the next animation frame callback (inserted by requestAnimationFrame function."

Am I right, or am I missing something important?

Kaiido
  • 123,334
  • 13
  • 219
  • 285
Byeongin Yoon
  • 3,233
  • 6
  • 26
  • 44

1 Answers1

2

The microtask queue is visited every time the JS call stack is empty, as part of clean after running a script.

  1. If the JavaScript execution context stack is now empty, perform a microtask checkpoint.

This step is entered in between all the animation callback execution, from the invoke algorithm.

Note that the event-loop processing also has a few hard-coded microtask checkpoints, e.g at step 7, but also in other callbacks execution.

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • Thanks Kaiido, your answer helps me a lot. I have an another question. In this [example code](https://codepen.io/_-0-_/pen/KKZpqMv) that you give me at this [question](https://stackoverflow.com/questions/71454458/practical-example-where-micro-task-queue-is-useful-in-event-loop) you said **click events are generally fired from an animation frame**, but It is really hard to find about it to me. I'm reading [DOM Event Architecture](https://w3c.github.io/uievents/#dom-event-architecture) and [Event Handlers](https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-attributes), but hard – Byeongin Yoon Mar 23 '22 at 03:24
  • 1
    Yeah, it's more by experience running a lot of tests that I noticed this is the case. Mousemove events and other pointer events [are throttled to animated frames](https://stackoverflow.com/questions/57711515/javascript-eventlistener-pointermove-points-per-second/57719951#57719951), I think click aren't really but in most of the tests I performed the results were as if they were, (at least in Chrome and FF). But note that this doesn't mean it's part of the rAF callbacks, it's just that they wait for the "animation frame" to execute the task. – Kaiido Mar 23 '22 at 05:03
  • I finished a presentation about event loop. I really feel thanks to you. Have a nice day. – Byeongin Yoon Apr 01 '22 at 05:04
  • You're welcome. By curiosity, where was this presentation? In Seoul? I might be interested in visiting someday. – Kaiido Apr 01 '22 at 05:20
  • 1
    Hello Kaiido!! Sorry for late comment. this is the [youtube link](https://www.youtube.com/watch?v=YpQTeIqjC4o) of my presentation. But, this is korean. If you live in Seoul, I want to meet you. Would you please send me an email (rpf5573@kakao.com) ? – Byeongin Yoon Jul 15 '22 at 11:49