当前位置: 首页 > news >正文

事件循环机制及常见面试题

借鉴:

《Javascript 忍者秘籍》第二版,事件循环篇

面试 | JS 事件循环 event loop 经典面试题含答案 - 知乎 (zhihu.com)

概念

  1. 主栈队列就是一个宏任务,每一个宏任务执行完就会执行宏任务中的微任务,直到微任务全部都执行完,才开始执行下一个宏任务。
  2. JS 中任务的执行顺序优先级是:主栈全局任务(宏任务) > 宏任务中的微任务 > 下一个宏任务。,所以 promise(微任务).then() 的执行顺序优先级高于setTimeout定时器。
  3. 不能满目的将 .then 的回调放入微任务队列;因为没有调用 resolve或者reject 之前是不算异步任务完成的, 所以不能将回调随意的放入微任务事件队列
  4. await 是一个让出线程的标志。await 后面的表达式会先执行一遍,将 await 后面的代码加入到 micro task中这个微任务是 promise 队列中微任务,然后就会跳出整个 async 函数来继续执行后面的代码。
  5. process.nextTick 是一个独立于 eventLoop 的任务队列,主栈中的宏任务每一次结束后都是先执行 process.nextTick队列,在执行微任务 promise 的 .then()
  6. 每一个宏任务和宏任务的微任务执行完后都会对页面 UI 进行渲染
  • 宏任务 macrotask
    • script 整体代码
    • setTimeout
    • setInterval
    • setImmediate
    • I/O
    • ui render
  • 微任务 microtask
    • process.nextTick
    • promise.then
    • await 后面的代码
    • MutationObserver(h5新特性)

为什么 await 后面的代码会进入到promise队列中的微任务?

async/await 只是操作 promise 的语法糖,最后的本质还是promise
async function async1() {console.log('async1 start');await async2();console.log('async1 end');
}
// 上面的代码等价于 ==>
async function async1() {console.log('async1 start');Promise.resolve(async2()).then(() => {console.log('async1 end')})
}

面试题

面试题1

async function async1() {console.log('1') // 2async2().then(() => {console.log('2')})
}
async function async2() {console.log('3') // 3
}
console.log('4') // 1
setTimeout(function () {console.log('5')
}, 0)
async1();
new Promise(function (resolve) {console.log('6') // 4resolve();
}).then(function () {console.log('7')
})
console.log('8') // 5
//4 1 3 6 8 2 7 5

面试题2


setTimeout(() => {console.log(1);}, 0);async function main1() {new Promise((resolve, reject) => {console.log(2);resolve();}).then(() => {console.log(3);})await main2();console.log(7);}function main2() {console.log(8);}main1();setTimeout(() => {console.log(10);}, 0);//  
2
8
3
7
1
10

面试题3

Promise.resolve().then(() => {console.log(0);return Promise.resolve('4x');}).then((res) => { console.log(res) })
Promise.resolve().then(() => { console.log(1); }).then(() => { console.log(2); }, () => { console.log(2.1) }).then(() => { console.log(3); }).then(() => { console.log(5); }).then(() => { console.log(6); })// 0 1 2 3 4x 5 6

面试题4  详解:(1 封私信) 关于promise输出顺序的疑问? - 知乎 (zhihu.com)

new Promise((resolve,reject) => {console.log('外部promise')resolve()
})
.then(() => {console.log('外部第一个then')new Promise((resolve,reject) => {console.log('内部promise')resolve()}).then(() => {console.log('内部第一个then')return Promise.resolve()}).then(() => {console.log('内部第二个then')})
})
.then(() => {console.log('外部第二个then')
})
.then(() => {console.log('外部第三个then')
})
.then(() => {console.log('外部第四个then')
})	// 
外部promise外部第一个then内部promise
内部第一个then
外部第二个then
外部第三个then外部第四个then内部第二个then

面试题5

// A 任务
setTimeout(() => {console.log(1)
}, 20)// B 任务
setTimeout(() => {console.log(2)
}, 0)// C 任务
setTimeout(() => {console.log(3)
}, 10)// D
setTimeout(() => {console.log(5)
}, 10)console.log(4)
/* 输出
*   4 -> 2-> 3 -> 5 -> 1
*/

面试题6

setTimeout(function () {console.log(1)
}, 0);new Promise(function (resolve, reject) {console.log(2)for (var i = 0; i < 10000; i++) {if (i === 10) {console.log(10)}i == 9999 && resolve();}console.log(3)
}).then(function () {console.log(4)
})
console.log(5);
// 2, 10, 3, 5, 4, 1

面试题7

console.log("start");
setTimeout(() => {console.log("children2")Promise.resolve().then(() =>{console.log("children3")})
}, 0)new Promise(function(resolve, reject){console.log("children4")setTimeout(function(){console.log("children5")resolve("children6")}, 0)
}).then(res =>{         // flagconsole.log("children7")setTimeout(() =>{console.log(res)}, 0)
})
// start children4 children2 children3  children5  children7 children6

面试题8

这道题的难点在于是 promise2还是 async1 end 先输出。从全局宏任务之上而下执行时 await async2() 后面的代码 console.log('async1 end') 先进入 promise 中的微任务队列,最后.then() 中的console.log('promise2') 再进入到 promise 中的微任务队列。所以再开始下一轮宏任务循环之前先输出了 async1 end 再输出了 promise2。全局中的微任务执行完成开始下一轮宏任务setTimeout 最后输出 setTimeout

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((resolve) => {console.log('promise1')resolve()
}).then(function () {console.log('promise2')
})
console.log('script end')
//输出
//script start
//async1 start
//async2
//promise1
//script end
//async1 end
//promise2
//setTimeout

面试题9 

首先开始全局下的宏任务依次输出 script start, async1 start, promise1, promise3, script end。其中 await async2();async2() 中.then()的代码先进入到promise的微任务队列,await async2(); 后面的代码再进入到promise的任务队列,console.log('promise4'); 最后进入到 promise 的任务队列。全局下的宏任务结束,开始全局下的微任务,promise 的微任务队列中按照队列的先进先出原则依次输出,promise2,async1 end,promise4。全局微任务结束,开始下一轮的宏任务setTimeout,最终输出 setTimeout

async function async1() {console.log('async1 start');await async2();console.log('async1 end');
}
async function async2() {new Promise(function (resolve) {console.log('promise1');resolve();}).then(function () {console.log('promise2');});
}
console.log('script start');
setTimeout(function () {console.log('setTimeout');
}, 0)
async1();
new Promise(function (resolve) {console.log('promise3');resolve();
}).then(function () {console.log('promise4');
});
console.log('script end');
//script start, 
// async1 start, 
// promise1, 
// promise3, 
// script end, 
// promise2,
// async1 end,
// promise4, 
// setTimeout

面试题10

async function async1() {console.log('async1 start');await async2();setTimeout(function() {console.log('setTimeout1')  // 这一部分代码会放入到 promise 的微任务队列中。},0)
}
async function async2() {setTimeout(function() {console.log('setTimeout2')},0)
}
console.log('script start');
setTimeout(function() {console.log('setTimeout3');
}, 0)
async1();
new Promise(function(resolve) {console.log('promise1');resolve();
}).then(function() {console.log('promise2');
});
console.log('script end');
// script start, async1 start, promise1, script end,
// promise2, setTimeout3,  setTimeout2, setTimeout1

相关文章:

事件循环机制及常见面试题

借鉴&#xff1a; 《Javascript 忍者秘籍》第二版&#xff0c;事件循环篇 面试 | JS 事件循环 event loop 经典面试题含答案 - 知乎 (zhihu.com) 概念 主栈队列就是一个宏任务&#xff0c;每一个宏任务执行完就会执行宏任务中的微任务&#xff0c;直到微任务全部都执行完&a…...

智能监控平台/视频共享融合系统EasyCVR接入RTSP协议视频流无法播放原因是什么?

视频集中存储/云存储/视频监控管理平台EasyCVR能在复杂的网络环境中&#xff0c;将分散的各类视频资源进行统一汇聚、整合、集中管理&#xff0c;实现视频资源的鉴权管理、按需调阅、全网分发、智能分析等。AI智能/大数据视频分析EasyCVR平台已经广泛应用在工地、工厂、园区、楼…...

c# statusStrip 显示电脑主机名、IP地址、MAC地址

控件&#xff1a; ToolStripStatusLabel 主机名&#xff1a; Dns.GetHostName() IP地址&#xff1a; Dns.GetHostAddresses(Dns.GetHostName())[0].ToString() 当前程序的版本&#xff1a; Assembly.GetExecutingAssembly().GetName().Version.ToString() 获取系统版本 …...

Cesium.CustomShader颜色值显示错误

官方示例&#xff1a; Cesium Sandcastle 测试过程&#xff1a; 1、修改示例&#xff0c;把customshader中的fragmentShaderText替换为如下代码 void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {//注意&#xff1a;下述颜色的b值是0.1&#x…...

XSLVGL2.0 User Manual 页面管理器(v2.0)

XSLVGL2.0 开发手册 XSLVGL2.0 User Manual 页面管理器 1、概述2、特性3、APIs3.1、xs_page_init3.2、xs_page_wait_inited3.3、xs_page_exit3.4、xs_page_acquire3.5、xs_page_release3.6、xs_page_set_bootlogo3.7、xs_page_setup_clear_finish3.8、xs_page_setup_is_finish…...

论文学习-Attention Is All You Need

Attention Is All You Need 目前暂时不会用到&#xff0c;大概了解一下即可。 Recurrent model 序列化的计算方式&#xff0c;难以并行&#xff0c;随着序列的增长&#xff0c;以前的记忆会逐渐丢失。而Attention机制可以观察到句子中所有的信息&#xff0c;不受距离影响&…...

Springboot 使用 RabbitMq 延迟插件 实现订单到期未支付取消订单、设置提醒消息

示例业务场景&#xff1a; 场景1&#xff1a;客户下单后&#xff0c;15分钟内未支付取消订单&#xff01; 场景2&#xff1a;客户下单支付成功后&#xff0c;5分钟内商家未处理订单&#xff0c;需要推送一条消息提醒商家。如依旧未处理&#xff0c;则需要每隔2分钟消息提醒一下…...

Linux安装Tesseract-OCR(操作系统CentOS)

Linux安装Tesseract-OCR 第一步&#xff0c;安装依赖第二步&#xff0c;下载安装包第三步&#xff0c;安装leptonica库第四步&#xff0c;安装tesseract第五步&#xff0c;添加语言包第六步&#xff0c;测试 第一步&#xff0c;安装依赖 sudo yum install libpng-devel rpm -q…...

pair和typedef

文章目录 一、pair用法1.2、pair的创建和初始化1.3、pair对象的操作1.4、(make_pair)生成新的pair对象1.5、通过tie获取pair元素值 2、typedef2.1、什么是typedef2.2、typedef用法2.2.1、对于数据类型使用例如&#xff1a;2.2.2、对于指针的使用例如2.2.3、对于结构体的使用 2.…...

rdf-file:分布式环境下的文件处理

一&#xff1a;简介 数据量大了以后&#xff0c;单机解析或者生成文件的效率就很低&#xff0c;需要通过集群处理&#xff1a; 机构过来的文件&#xff1a;我们先对文件进行分片&#xff0c;在利用集群集群处理分片文件。给机构文件&#xff1a;分库分表数据&#xff0c;每个…...

Maven下载与安装教程

一、下载 Maven 进入 Maven 官网&#xff1a;maven.apache.org/download.cgi 选择 .zip 文件下载&#xff0c;最新版本是 3.9.5 二、安装 Maven 将 .zip 文件解压到没有中文没有空格的路径下。例如下图&#xff0c;在创建一个repository的空文件夹在他的下面&#xff0c;用于…...

C++(20):通过starts_with/ends_with检查字符串

C20提供了starts_with用于检查字符串是否以某个字符串开始&#xff0c;ends_with用于检查是否以某个字符串结束&#xff1a; #include <iostream> #include <string> using namespace std;int main() {string str "hello and 88";cout<<str.star…...

YOLOv8+Nanodet强强联合改进标签分配:使用NanoDet动态标签分配策略,同时集成VFL全新损失,来打造新颖YOLOv8检测器

💡本篇内容:YOLOv8+Nanodet强强联合改进标签分配:使用NanoDet动态标签分配策略,同时集成VFL全新损失,来打造新颖YOLOv8检测器 💡🚀🚀🚀本博客 YOLO系列 + 改进NanoDet模型的动态标签分配策略源代码改进 💡一篇博客集成多种创新点改进:VFL损失函数 + Nanodet…...

base64字符串转成file

分割base64字符串&#xff0c;获取base64的格式和ASCII字符串&#xff1b;使用atob()方法将base64中的ASCII字符串解码成二进制数据"字符串"&#xff1b;将二进制数据按位放入8 位无符号整型数组中适用new File()方法将ArrayBuffer转换成file对象 const base64 &qu…...

NextJS开发:Prisma开启SQL日志输出

在 Prisma 中打印执行的 SQL 可以通过在 PrismaClient 实例上设置 log 配置参数来实现。具体步骤如下&#xff1a; 在你的Prisma项目根目录中&#xff0c;找到 prisma/schema.prisma 文件在 datasource 块中&#xff0c;找到你正在使用的数据库配置&#xff0c;并添加 provide…...

barcode.js+elementUi——实现二维码的展示——基础积累

barcode.js——实现二维码的展示——基础积累 CSDN服务器一直报错条形码需求分析1.barcode.js的引入2.html页面上的写法——我这边是一个elementUI的弹窗条形码3.script中的部分 CSDN服务器一直报错 最近不知道怎么了&#xff0c;CSDN一直报服务器错误&#xff0c;不能只有我自…...

vue2 el-table 封装

vue2 el-table 封装 在 custom 文件夹下面创建 tableList.vue直接上代码&#xff08;代码比较多&#xff0c;复制可直接用&#xff09; <template><div class"mp-list"><el-tableref"multipleTable"class"mp-custom-table":dat…...

harmonyos应用开发者高级认证考试部分答案(2)

一、判断 只要使用端云一体化的云端资源就需要支付费用&#xff08;错&#xff09; 所有使用Component修饰的自定义组件都支持onPageShow&#xff0c;onBackPress和onPageHide生命周期函数。&#xff08;错&#xff09; HarmonyOS应用可以兼容OpenHarmony生态&#xff08;对&am…...

【物联网与大数据应用】Hadoop数据处理

Hadoop是目前最成熟的大数据处理技术。Hadoop利用分而治之的思想为大数据提供了一整套解决方案&#xff0c;如分布式文件系统HDFS、分布式计算框架MapReduce、NoSQL数据库HBase、数据仓库工具Hive等。 Hadoop的两个核心解决了数据存储问题&#xff08;HDFS分布式文件系统&#…...

Kotlin学习——kt里的集合List,Set,Map List集合的各种方法之Int篇

Kotlin 是一门现代但已成熟的编程语言&#xff0c;旨在让开发人员更幸福快乐。 它简洁、安全、可与 Java 及其他语言互操作&#xff0c;并提供了多种方式在多个平台间复用代码&#xff0c;以实现高效编程。 https://play.kotlinlang.org/byExample/01_introduction/02_Functio…...

mongodb源码分析session执行handleRequest命令find过程

mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程&#xff0c;并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令&#xff0c;把数据流转换成Message&#xff0c;状态转变流程是&#xff1a;State::Created 》 St…...

AtCoder 第409​场初级竞赛 A~E题解

A Conflict 【题目链接】 原题链接&#xff1a;A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串&#xff0c;只有在同时为 o 时输出 Yes 并结束程序&#xff0c;否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

Linux云原生安全:零信任架构与机密计算

Linux云原生安全&#xff1a;零信任架构与机密计算 构建坚不可摧的云原生防御体系 引言&#xff1a;云原生安全的范式革命 随着云原生技术的普及&#xff0c;安全边界正在从传统的网络边界向工作负载内部转移。Gartner预测&#xff0c;到2025年&#xff0c;零信任架构将成为超…...

解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错

出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上&#xff0c;所以报错&#xff0c;到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本&#xff0c;cu、torch、cp 的版本一定要对…...

中医有效性探讨

文章目录 西医是如何发展到以生物化学为药理基础的现代医学&#xff1f;传统医学奠基期&#xff08;远古 - 17 世纪&#xff09;近代医学转型期&#xff08;17 世纪 - 19 世纪末&#xff09;​现代医学成熟期&#xff08;20世纪至今&#xff09; 中医的源远流长和一脉相承远古至…...

网站指纹识别

网站指纹识别 网站的最基本组成&#xff1a;服务器&#xff08;操作系统&#xff09;、中间件&#xff08;web容器&#xff09;、脚本语言、数据厍 为什么要了解这些&#xff1f;举个例子&#xff1a;发现了一个文件读取漏洞&#xff0c;我们需要读/etc/passwd&#xff0c;如…...

短视频矩阵系统文案创作功能开发实践,定制化开发

在短视频行业迅猛发展的当下&#xff0c;企业和个人创作者为了扩大影响力、提升传播效果&#xff0c;纷纷采用短视频矩阵运营策略&#xff0c;同时管理多个平台、多个账号的内容发布。然而&#xff0c;频繁的文案创作需求让运营者疲于应对&#xff0c;如何高效产出高质量文案成…...

MySQL 知识小结(一)

一、my.cnf配置详解 我们知道安装MySQL有两种方式来安装咱们的MySQL数据库&#xff0c;分别是二进制安装编译数据库或者使用三方yum来进行安装,第三方yum的安装相对于二进制压缩包的安装更快捷&#xff0c;但是文件存放起来数据比较冗余&#xff0c;用二进制能够更好管理咱们M…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...