4

For example, Promises use microtasks, and I verified here that they can be fullfilled before an animation frame is over (in Chrome). I am talking about frames made with requestAnimationFrame.

I am wondering what guarantee we have as that microtasks will fire within the same animation frame, after some logic that queued the microtask and before the end of the animation frame (f.e. when resolving a promise inside of an animation frame).

If there is some level of guarantee, then I believe that this lends to an answer for Does MutationOberserver handler fire within the same animation frame?. This might even be the same question (indirectly).

Community
  • 1
  • 1
trusktr
  • 44,284
  • 53
  • 191
  • 263

1 Answers1

0

seems like it is

try

function a() {
    requestAnimationFrame(b=>{
        console.log(b, 'rAF');
        Promise.resolve(1).then(()=>console.log('promise'));
    })}
a();a();

and you will get

585838.7970909999 rAF
promise
585838.7970909999 rAF
promise
Austaras
  • 901
  • 8
  • 24
  • 1
    Turns out, immediate microtasks (f.e. Promise.resolve()) always fire right after the macrotask that launched them, and if those microtasks launch more microtasks, then those new ones fire right after the current microtask. So it is possible to make a microtask infinite loop that will lock the CPU at 100%, thus no other macrotask code (f.e. another animation frame or timeout) will ever fire. We can think of it almost like synchronous code that is re-ordered to the end of the currently executing synchronous code. – trusktr Oct 09 '18 at 23:46