当前位置:网站首页>事件队列、微任务与宏任务的理解和面试题
事件队列、微任务与宏任务的理解和面试题
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");边栏推荐
- Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 1)
- 一文详解|Go 分布式链路追踪实现原理
- Practical application of "experience" crawler in work
- 「经验」爬虫在工作中的实战应用『实现篇』
- Swin-Transformer(2021-08)
- Personally test the size of flutter after packaging APK, quite satisfied
- Teach you how to write selenium test cases
- 全技术栈、全场景、全角色云原生系列培训重磅首发,助力企业打造硬核云原生技术团队
- 究竟有哪些劵商推荐?另外,手机开户安全么?
- Word -- a solution for word to encounter errors when trying to open a file
猜你喜欢

Lenovo Yoga 27 2022, full upgrade of super configuration

The cloud native landing practice of using rainbow for Tuowei information

熵-条件熵-联合熵-互信息-交叉熵

设计电商秒杀系统

Cloud Native Landing Practice Using rainbond for extension dimension information

Brief introduction of Feature Engineering in machine learning

拓维信息使用 Rainbond 的云原生落地实践

小小笔记-整型提升(C语言)

Entropy - conditional entropy - joint entropy - mutual information - cross entropy

sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
随机推荐
js 字符串截取方法汇总
Some interesting modules
基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
A common mistake in enterprise model selection
Coding officially entered Tencent conference application market!
slice
mysql中union和union all的区别
阿里天池SQL训练营学习笔记5
Four tips tell you how to use SMS to promote business sales?
MQ优缺点(2022.5.2-5.8)
Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched
Nodejs installation and introduction
Build graphql service based on Actix, async graphql, rbatis, pgsql/mysql (4) - change service
BeanUtils.copyProperties() 对比 mapstruct
教你Selenium 测试用例编写
【DesignMode】单例模式(singleton pattern)
20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
rust配置国内源
小小笔记-整型提升(C语言)
How does rust implement dependency injection?