当前位置: 首页 > 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…...

Windows和Office激活终极解决方案:KMS_VL_ALL_AIO完全指南

Windows和Office激活终极解决方案&#xff1a;KMS_VL_ALL_AIO完全指南 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 你是否厌倦了Windows系统频繁弹出激活提醒&#xff1f;是否因为Office软件…...

南麟 SA9120 单通道 H 桥直流有刷电机驱动专用芯片 SOP8封装

产品描述南麟 SA9120 是上海南麟电子推出的单通道 H 桥直流有刷电机驱动专用芯片&#xff0c;采用成熟高压工艺设计&#xff0c;集成度高、外围电路简单&#xff0c;可直接驱动中小功率直流电机、步进电机绕组及电磁阀等感性负载&#xff0c;具备优异的抗干扰与带载能力&#x…...

实战部署JetBrains IDE试用期重置:自动化清理与插件开发全流程

实战部署JetBrains IDE试用期重置&#xff1a;自动化清理与插件开发全流程 【免费下载链接】ide-eval-resetter 项目地址: https://gitcode.com/gh_mirrors/id/ide-eval-resetter JetBrains IDE试用期重置工具是一个开源项目&#xff0c;专门用于清除IntelliJ IDEA、Py…...

网络钓鱼攻击特征识别与多维度防御技术研究

摘要 网络钓鱼是当前最普遍、危害最突出的网络安全威胁类型&#xff0c;攻击者依托伪造邮件、短信、社交信息与虚假网站&#xff0c;结合社会工程学手段诱导用户泄露敏感数据或执行恶意操作&#xff0c;对个人信息安全与机构运行稳定构成持续威胁。本文以网络钓鱼攻击机理、典型…...

MiniCPM-o-4.5-nvidia-FlagOS插件开发指南:为谷歌浏览器打造智能阅读与摘要助手

MiniCPM-o-4.5-nvidia-FlagOS插件开发指南&#xff1a;为谷歌浏览器打造智能阅读与摘要助手 你是不是经常在网上冲浪时&#xff0c;面对一篇长文感到头疼&#xff0c;只想快速抓住核心要点&#xff1f;或者遇到一篇外文资料&#xff0c;需要逐句翻译才能理解&#xff1f;又或者…...

百考通:AI精准驱动数据分析,让数据价值更具人工写作的温度与逻辑

在数字化浪潮席卷各行各业的今天&#xff0c;数据已成为核心生产要素&#xff0c;但如何从海量数据中挖掘价值、辅助决策&#xff0c;始终是企业与个人面临的核心难题。传统数据分析流程繁琐、技术门槛高、周期漫长&#xff0c;让许多非专业人士望而却步。百考通&#xff08;ht…...

Fluxion多语言支持终极指南:从.lang文件到本地化shell脚本的完整实现

Fluxion多语言支持终极指南&#xff1a;从.lang文件到本地化shell脚本的完整实现 【免费下载链接】fluxion Fluxion is a remake of linset by vk496 with enhanced functionality. 项目地址: https://gitcode.com/gh_mirrors/fl/fluxion Fluxion是一款功能强大的无线网…...

Android AdvancedRecyclerView与LiveData结合:实现数据实时更新的终极指南

Android AdvancedRecyclerView与LiveData结合&#xff1a;实现数据实时更新的终极指南 【免费下载链接】android-advancedrecyclerview RecyclerView extension library which provides advanced features. (ex. Googles Inbox app like swiping, Play Music app like drag and…...

GLM-4.1V-9B-Base与Dify联动:零代码构建企业级AI应用平台

GLM-4.1V-9B-Base与Dify联动&#xff1a;零代码构建企业级AI应用平台 1. 企业AI应用的新选择 最近接触了不少企业客户&#xff0c;发现一个普遍现象&#xff1a;大家都想用AI&#xff0c;但真正能用起来的却不多。技术门槛高、开发周期长、维护成本大&#xff0c;这些问题让很…...

游戏化编程革命:CodeCombat如何破解传统编程教学的三大难题

游戏化编程革命&#xff1a;CodeCombat如何破解传统编程教学的三大难题 【免费下载链接】codecombat Game for learning how to code. 项目地址: https://gitcode.com/gh_mirrors/co/codecombat 在数字化浪潮席卷全球的今天&#xff0c;编程已成为21世纪的核心素养&…...