当前位置:网站首页>JS macro task and micro task
JS macro task and micro task
2022-07-28 05:54:00 【Quiet sky】
One 、 About js Perform asynchronous task explanation
- js yes Single thread language ( If multithreading dom Will go crazy )
- js Although it's single threaded , however Browsers are multithreaded ,js Encounter asynchronous tasks , I didn't deal with it myself , Instead, it is handed over to other threads of the browser
- Browser threads include :javascript Engine threads 、 Interface rendering thread 、 Browser event trigger thread 、 Program timer thread 、http Request thread, etc
- js Common asynchronous tasks are : event 、 Timer 、 Network request, etc
Two 、js The event loop
- Single thread means , All tasks are queued , The previous task is over , Will perform the latter task
- So you can only perform one task at a time , Called the main thread , Used to perform synchronization tasks
- Outside the main thread , There are also two task lists for storing asynchronous tasks , Macro task 、 Micro task
- once “ Execution stack ” All synchronization tasks in are completed , The system will read “ Task queue ”, The corresponding event enters “ Execution stack ” Start execution , The order of execution is : The main thread => Micro task => Macro task
- The main thread keeps repeating the third step above , That's what they say Event Loop( The event loop )
Macro task : Including the overall code script,setTimeout,setIntervcal
Micro task :Promise,process.nextTick


3、 ... and 、 Practical cases
console.log('1');
setTimeout(function() {
console.log('2');
process.nextTick(function() {
console.log('3');
})
new Promise(function(resolve) {
console.log('4');
resolve();
}).then(function() {
console.log('5')
})
})
process.nextTick(function() {
console.log('6');
})
new Promise(function(resolve) {
console.log('7');
resolve();
}).then(function() {
console.log('8')
})
setTimeout(function() {
console.log('9');
process.nextTick(function() {
console.log('10');
})
new Promise(function(resolve) {
console.log('11');
resolve();
}).then(function() {
console.log('12')
})
})
//js The code is from top to bottom , Line by line points to , First encounter the first line console.log(‘1’); Execution output 1, And then the second line setTimeout Asynchronous task , Put in task queue .
// Next, I met promise.nextTick It is the micro task that is put at the end of this cycle , And then I met new Promise Direct to output 7,then Put to the end of this cycle , Then perform
// Encountered again setTimeout Put in task queue , This round of code execution is finished , Start executing the code at the end of this round in turn , Output 6,8. Then the task of the main thread is completed , No task queue
// Take out one of setTimeout Put it into the main thread and start execution , Output 2, And then meet process.nextTick, At the end of this cycle , perform new Promise Output 4,then
// Put it at the end of this cycle , The main thread code has finished executing , Start executing the output at the end of this round 3,5. Then go to the task queue to get the second setTimeout Execution output 9,11,10,12.
// So the order of output is 1,7,6,8,2,4,3,5,9,11,10,12.
边栏推荐
- Screenshot transferred to the background
- 蓝桥代码 错误票据
- 结果填空 国庆有几天是星期日(纯Excel解决)
- 结果填空 凑算式(DFS*C语言)
- Community epidemic access management system based on PHP (PHP graduation design)
- GIS领域竞赛整理(不完全统计)
- The difference between null and undefined
- Making E-R diagram based on XMIND
- 基于php小区疫情出入管理系统(php毕业设计)
- 单调队列,洛谷P1886 滑动窗口
猜你喜欢
随机推荐
常见WAF拦截页面总结
对象内多个数组的对应下标相加
360兼容问题
(php毕业设计)基于php在线旅游网站管理系统获取
Annotation and grid addition of ArcGIS map making
基于php心理健康服务系统获取(php毕业设计)
Time setting in curd component
Global event bus
(PHP graduation project) obtained based on PHP student homework submission management system
书籍-邓普顿教你逆向
ArcGIS Engine开发资源
DOM——页面的渲染、style属性操作、预加载与懒加载、防抖与节流
初学移动端
ArcGIS之Model Builder
简单几步实现小程序分享朋友圈
js-简单的发布订阅类
【博学谷学习记录】超强总结,用心分享 | 集合
Thinking on Architecture Design (SSO design)
Distance toolbar in ArcMap (distance)
js-宏任务和微任务









