We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
简单来说,每一轮事件循环将会: 1.以队列的顺序执行先进入队列的宏任务 2.以队列的顺序执行完当前宏任务的所有微任务 3.以队列的顺序执行下一个宏任务
执行以下代码将会输出什么?
var foo = () => (new Promise((resolve, reject) => { console.log(1); let p = new Promise((resolve, reject) => { console.log(2); setTimeout(() => { console.log(3); }, 0) resolve(4); }); resolve(5); p.then((arg) => { console.log(arg); }); })); foo().then((arg) => { console.log(arg); }); console.log(6);
事件循环第一轮:立即输出1,2,6。然后在本轮的微任务队列里输出4,5。
事件循环第二轮:执行宏任务队列中setTimeout的函数体,输出3。
代码执行后依次输出1,2,6,4,5,3
Concurrency model and Event Loop-MDN Difference between microtask and macrotask within an event loop context
The text was updated successfully, but these errors were encountered:
博主写的精炼,刚好最近遇到事件循环的问题,看完觉得总结的不错,哈哈
Sorry, something went wrong.
No branches or pull requests
规范
事件循环
简单来说,每一轮事件循环将会:
1.以队列的顺序执行先进入队列的宏任务
2.以队列的顺序执行完当前宏任务的所有微任务
3.以队列的顺序执行下一个宏任务
例子
执行以下代码将会输出什么?
事件循环第一轮:立即输出1,2,6。然后在本轮的微任务队列里输出4,5。
事件循环第二轮:执行宏任务队列中setTimeout的函数体,输出3。
代码执行后依次输出1,2,6,4,5,3
参考文章
Concurrency model and Event Loop-MDN
Difference between microtask and macrotask within an event loop context
The text was updated successfully, but these errors were encountered: