先看一段代码javascript
setTimeout(function() { console.log('timeout'); }, 0); new Promise((resolve, reject) => { console.log('promise'); resolve(); }).then(function() { console.log('then'); }); console.log('global');
输出: promise 、global、 then、timeout
html
在 V8 实现中包含两种任务:html5
script
,setTimeout
, setInterval
, setImmediate
, I/O
, UI rendering
java
process.nextTick
, Promises
, Object.observe
, MutationObserver
git
注意:es6
- 从上面能够看到,microtask 会执行完毕才进行渲染,若是 microtask 执行时间教长,会致使卡顿
- 上面执行过程只是 chrome 的,safri 又不太同样
执行过程的示例 :
https://jakearchibald.com/201...github
new MutationObserver(function() { console.log('mutate'); }).observe(document.body, { attributes: true }); document.body.setAttribute('data-random', Math.random()); setTimeout(function() { console.log('timeout'); }, 0); new Promise((resolve, reject) => { console.log('promise'); resolve(); }).then(function() { console.log('then'); }); console.log('global');
输出: promise 、global、mutate、 then、timeoutchrome
microTask
: https://github.com/kaerus-com... es-promise
: https://github.com/stefanpenn...