当前位置:网站首页>Browser node event loop
Browser node event loop
2022-07-02 12:43:00 【There is no water in the sea】
javascript In the browser JavaScript Threads

1、 Browser's event loop

2、 Macro task and micro task
1、 Timer 、ajax、DOM Waiting for the callback will be added to the macro task
2、queueMicrotask、Promise then When you join the micro task
notes : Before executing any macro tasks , We need to ensure that the micro task queue has been emptied .

3、Promise Interview questions
3.1
setTimeout(function () {
console.log("setTimeout1");
// Constructor code will not be added to the task queue , Will be put into main script, Will be executed directly
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");
});
// Output :
promise1
2
then1
queueMicrotask
then3
setTimeout1
then2
then4
setTimeout2
3.2
async function bar() {
console.log("2222222");
return new Promise((resolve) => {
resolve();
});
}
async function foo() {
console.log("1111111");
await bar();
// 3333 The execution of the must wait until bar() Called resovle Will execute .
// Because it's changed resolve, So you can 3333 It's being put in then() Inside the , Be put into micro tasks
console.log("3333333");
}
foo();
console.log("4444444");
// Output :
// 1111111
// 2222222
// 4444444
// 3333333
3.3
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, reject) {
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
4、Node The event loop
边栏推荐
- Deep Copy Event bus
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- Simple use of drools decision table
- 软件测试面试题-2022年大厂面试题合集
- Oracle从入门到精通(第4版)
- Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
- 中国交通标志检测数据集
- Redis avalanche, penetration, breakdown
猜你喜欢
![JDBC 预防sql注入问题与解决方法[PreparedStatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC 预防sql注入问题与解决方法[PreparedStatement]

JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)

堆 AcWing 839. 模拟堆
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

spfa AcWing 851. SPFA finding the shortest path

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

3 A VTT端接 稳压器 NCP51200MNTXG资料

spfa AcWing 851. spfa求最短路

线性DP AcWing 896. 最长上升子序列 II

上手报告|今天聊聊腾讯目前在用的微服务架构
随机推荐
JSON serialization and parsing
Dijkstra AcWing 850. Dijkstra求最短路 II
哈希表 AcWing 841. 字符串哈希
计数类DP AcWing 900. 整数划分
Adding database driver to sqoop of cdh6
Heap acwing 839 Simulated reactor
IPhone 6 plus is listed in Apple's "retro products" list
China traffic sign detection data set
Wechat official account payment prompt MCH_ ID parameter format error
软件测试面试题-2022年大厂面试题合集
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
Record the range of data that MySQL update will lock
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
正确遍历EntryList方法
Writing method of then part in drools
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
哈希表 AcWing 840. 模拟散列表
线性DP AcWing 895. 最长上升子序列
Visual studio efficient and practical extension tools and plug-ins
3 A VTT端接 稳压器 NCP51200MNTXG资料