当前位置:网站首页>事件队列、微任务与宏任务的理解和面试题
事件队列、微任务与宏任务的理解和面试题
2022-06-30 18:29:00 【至尊绝伦】
事件队列是一种数据结构,可以存放要执行的任务。它符合队列"先进先出"的特点。
事件循环中并非只维护着一个队列,事实上是有两个队列:
- 宏任务队列(macrotask queue): ajax、setTimeout、setInterval、DOM监听、UI Rendering等
- 微任务队列(microtask queue): Promise的then回调、Mutation Observer API、queueMicrotask()
事件循环对于两个队列的优先级是怎么样的?
1.main script中的代码优先执行;
2.在执行任何一个宏任务之前(不是队列,是一个宏任务),都会先查看微任务队列中是否有任务需要执行
也就是宏任务执行之前,必须保证宏任务队列是空的;
如果不为空,那么就优先执行微任务队列中的任务(回调);
Promise面试题一
setTimeout(function(){
console.log("setTimeout1");
new Promise(function (resolve){
resolve()
}).then(function(){
new Promise(function(resolve){
resolve()
}).then(function(){
console.log("then4");
})
console.log("then2");
})
})
new Promise(function(resolve){
console.log("promise1");
resolve()
}).then(function(){
console.log("then1");
})
setTimeout(function(){
console.log("setTimeout2");
})
console.log(2);
queueMicrotask(()=> {
console.log("queueMicrotask");
})
new Promise(function(resolve){
resolve()
}).then(function() {
console.log("then3");
})promise async await面试题二
async function async1() {
console.log("async1 start");
await async2()
console.log("async1 end");
}
async function async2() {
console.log("async2");
}
console.log("script start");
setTimeout(function(){
console.log("setTimeout")
}, 0)
async1()
new Promise( function (resolve) {
console.log("promise");
resolve()
}).then(function(){
console.log("promise2");
})
console.log("script end");边栏推荐
- slice
- nats集群部署
- Nodejs 安装与介绍
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- Pyth Solana is a bridge connecting reality on the chain
- How to use xUnit framework to maintain test cases?
- 一文详解|Go 分布式链路追踪实现原理
- MySQL function to get the full path
- Go redis connection pool
- 阿里天池SQL训练营学习笔记5
猜你喜欢

Huaxing Securities: kitex practice under the original hybrid Cloud Architecture

虚拟主机什么时候适合更换成云主机?

4个技巧告诉你,如何使用SMS促进业务销售?

ArcGIS无插件加载(无偏移)天地图

小球大小随机,随机运动碰撞

Evolution of screen display technology

sql连续登录问题

年复一年,为什么打破数据孤岛还是企业发展的首要任务
![Delete duplicate elements in the sorting linked list ii[unified operation of linked list nodes --dummyhead]](/img/dd/7df8f11333125290b4b30183cfff64.png)
Delete duplicate elements in the sorting linked list ii[unified operation of linked list nodes --dummyhead]

如何使用物联网低代码平台进行服务管理?
随机推荐
Swin-Transformer(2021-08)
Video content production and consumption innovation
如何利用 xUnit 框架对测试用例进行维护?
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 1)
JVM常见问题
开发那些事儿:Linux系统中如何安装离线版本MySQL?
Some interesting modules
Nodejs 安装与介绍
Development: how to install offline MySQL in Linux system?
Iris, the web framework of go
Temperature measuring instrument based on STM32 single chip microcomputer
Large file transfer software based on UDP protocol
Regular expressions (regular matching)
sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
Four tips tell you how to use SMS to promote business sales?
教你Selenium 测试用例编写
RFFE中MIPI协议
Kalman滤波器--从高斯融合推导
Mipi protocol in RFFE