为什么这个微任务在事件循环中先于宏任务执行?
Why is this microtask executed before macrotask in event loop?
我的理解是完整的微任务任务队列在每个宏任务之后被处理。
如果是这样,为什么 setTimeout
回调在以下 JavaScript
片段中的 Promise
微任务之后执行?
console.log('start');
setTimeout(() => {
console.log("setTimeout");
});
Promise.resolve().then(function() {
console.log('promise');
});
console.log('end');
这将输出以下内容:
> "start"
> "end"
> "promise"
> "setTimeout"
是不是因为现代浏览器造成的 ~4ms
延迟?
来自MDN:
In modern browsers, setTimeout()
/setInterval()
calls are throttled to
a minimum of once every 4ms when successive calls are triggered due to
callback nesting (where the nesting level is at least a certain
depth), or after certain number of successive intervals.
尽管这表明它仅适用于连续的回调嵌套。
My understanding is that the full microtask task queue is processed after each macrotask.
是的。而你运行,从console.log('start')
到console.log('end')
,就是这样的宏任务的代码。 运行 完成后,处理带有承诺回调的微任务队列,只有在那之后下一个宏任务(超时)才会到达 运行。
我的理解是完整的微任务任务队列在每个宏任务之后被处理。
如果是这样,为什么 setTimeout
回调在以下 JavaScript
片段中的 Promise
微任务之后执行?
console.log('start');
setTimeout(() => {
console.log("setTimeout");
});
Promise.resolve().then(function() {
console.log('promise');
});
console.log('end');
这将输出以下内容:
> "start"
> "end"
> "promise"
> "setTimeout"
是不是因为现代浏览器造成的 ~4ms
延迟?
来自MDN:
In modern browsers,
setTimeout()
/setInterval()
calls are throttled to a minimum of once every 4ms when successive calls are triggered due to callback nesting (where the nesting level is at least a certain depth), or after certain number of successive intervals.
尽管这表明它仅适用于连续的回调嵌套。
My understanding is that the full microtask task queue is processed after each macrotask.
是的。而你运行,从console.log('start')
到console.log('end')
,就是这样的宏任务的代码。 运行 完成后,处理带有承诺回调的微任务队列,只有在那之后下一个宏任务(超时)才会到达 运行。