Are microtasks guaranteed to fire within the same

2019-07-20 09:09发布

问题:

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).

回答1:

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