js浏览器环境

image.png
代码中分 宏任务 与 微任务

  • 宏任务: 整体代码script、 setTimeout、setInterval
  • 微任务:Promise

**
image.png

nodejs环境

image.png
image.png
image.png
如果非要一定先执行 setImmediate 后执行 setTimeout

  • 将他们放入 一个 I/O 队列中

    1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1630460391085-72d456b8-ca64-4a40-a251-c1af182d9b1d.png#clientId=u3a71ce46-0e19-4&from=paste&height=269&id=uf27dec6d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=538&originWidth=716&originalType=binary&ratio=1&size=198511&status=done&style=none&taskId=u4d34384d-3859-47bb-9c81-cd219b82294&width=358)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1630460432665-69e1f675-5cd6-407a-9e14-6220eca502f6.png#clientId=u3a71ce46-0e19-4&from=paste&height=309&id=u52c4fdc3&margin=%5Bobject%20Object%5D&name=image.png&originHeight=618&originWidth=1580&originalType=binary&ratio=1&size=358340&status=done&style=none&taskId=u114f2f08-b419-4973-9d25-8d3198f9248&width=790)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1612147502774-7e2f617a-baa9-498b-9994-c538d4815711.png#height=321&id=VqdIV&margin=%5Bobject%20Object%5D&name=image.png&originHeight=487&originWidth=995&originalType=binary&ratio=1&size=231158&status=done&style=none&width=655)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1612108365599-b57248bc-c48f-4215-9192-c4dfefc9986a.png#height=166&id=YPjmy&margin=%5Bobject%20Object%5D&name=image.png&originHeight=177&originWidth=742&originalType=binary&ratio=1&size=39544&status=done&style=none&width=695)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1612109336571-fa8196c8-98b8-470d-86da-fbba44f3530a.png#height=408&id=yEcCb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=602&originWidth=661&originalType=binary&ratio=1&size=135332&status=done&style=none&width=448)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12492651/1612109808166-66a0f187-2699-41f4-b536-3e81c948e9f9.png#height=250&id=DBpGv&margin=%5Bobject%20Object%5D&name=image.png&originHeight=427&originWidth=794&originalType=binary&ratio=1&size=75303&status=done&style=none&width=465)

    考点

  • Promise 和 nextTick 谁快? ```javascript new Promise(resolve => { console.log(‘Promise’) resolve() }) .then(function () { console.log(‘promise1’) }) .then(function () { console.log(‘promise2’) })

process.nextTick(() => { console.log(‘nextTick’) }) ```