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
在javascript中了解事件机制非常重要,它能帮我们更好清楚javascript本身。
了解了microtasks 和 macrotasks,javascript事件也大致清楚了。类别如下:
microtasks
macrotasks
在程序运行中表现有何异同呢?
console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0); Promise.resolve().then(function() { console.log('promise1'); }).then(function() { console.log('promise2'); }); console.log('script end');
例子中包含三部分:
运行结果如下:
script start script end promise1 promise2 setTimeout
规范中的microtasks和macrotasks
一个事件循环(event loop)会有一个或多个任务队列(task queue) task queue 就是 macrotask queue 每一个 event loop 都有一个 microtask queue task queue == macrotask queue != microtask queue 一个任务 task 可以放入 macrotask queue 也可以放入 microtask queue 中 当一个 task 被放入队列 queue(macro或micro) 那这个 task 就可以被立即执行了
在上面的理论基础上,分析如下
cycle1 :
script start
script end
cycle2 :
promise1
promise2
setTimeout
参考
The text was updated successfully, but these errors were encountered:
No branches or pull requests
event-loop microtasks & macrotasks
在javascript中了解事件机制非常重要,它能帮我们更好清楚javascript本身。
了解了
microtasks
和macrotasks
,javascript事件也大致清楚了。类别如下:macrotasks
microtasks
在程序运行中表现有何异同呢?
例子中包含三部分:
运行结果如下:
规范中的microtasks和macrotasks
在上面的理论基础上,分析如下
cycle1 :
script start
script end
正常打印cycle2 :
promise1
, 当前js task中为 Promise回调, 返回值为 undefined,当前 microtask执行完毕。promise2
。目前整个任务完毕。继续执行下一个task(macrotask)setTimeout
参考
对应中文
The text was updated successfully, but these errors were encountered: