Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$nextTick关于microtask优先于 (macro)task执行 #130

Closed
lz-lee opened this issue Jul 21, 2018 · 3 comments
Closed

$nextTick关于microtask优先于 (macro)task执行 #130

lz-lee opened this issue Jul 21, 2018 · 3 comments

Comments

@lz-lee
Copy link
Contributor

lz-lee commented Jul 21, 2018

nextTick 的实现

当调用栈空闲后每次事件循环只会从(macro)task 中读取一个任务并执行,而在同一次事件循环内会将 microtask 队列中所有的任务全部执行完毕,且要先于 (macro)task。

执行完一次(macro)task任务之后,会执行所有microtask。microtask优先于第二次(macro)task任务执行是否更合理。

@HcySunYang
Copy link
Owner

Please check: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

@lz-lee
Copy link
Contributor Author

lz-lee commented Jul 25, 2018

原来整体的script代码执行也是macroTask,那么microTask优先于macroTask执行也就说得通了。

@lz-lee lz-lee closed this as completed Jul 25, 2018
@lbwa
Copy link

lbwa commented Sep 5, 2018

根据 Tasks, microtasks, queues and schedules 中的介绍 Microtasks 中的第二段最后一句话:

microtasks always happen before the next task.

根据 W3CWHATWG living standard 所描述的 event loops processing model 中,每次 event loops 是以一个 (marco)task 开始,之后才是 microtask

个人仍然觉得 @lz-lee 所言的当前 event loop 中的 microtask 先于 下一次 (macro)task任务执行更加合理,并且全局执行上下文 script 作为包含初始代码执行环境的上下文,并不影响 (marco)task 在当次 event loop 先于 microtask 执行的规则,即当前 event loop 中的 microtask 先于 下一次 macrotask 更加合理?

还是我理解有误? @HcySunYang @lz-lee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants