当前位置:网站首页>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
边栏推荐
- 线性DP AcWing 895. 最长上升子序列
- Rust search server, rust quick service finding tutorial
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- Execute any method of any class through reflection
- std::vector批量导入快速去重方法
- Simple understanding of ThreadLocal
- Oracle从入门到精通(第4版)
- spfa AcWing 852. SPFA judgement negative ring
- VLAN experiment
- Simple use of drools decision table
猜你喜欢
移动式布局(流式布局)
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
Enhance network security of kubernetes with cilium
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
Use sqoop to export ads layer data to MySQL
BOM DOM
async/await 异步函数
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
VLAN experiment
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
随机推荐
Redis introduction, scenario and data type
Input box assembly of the shutter package
BOM DOM
[FFH] little bear driver calling process (take calling LED light driver as an example)
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
Wechat official account payment prompt MCH_ ID parameter format error
模块化 CommonJS ES Module
Docsify deploy IIS
堆 AcWing 839. 模拟堆
China traffic sign detection data set
Drools terminates the execution of other rules after executing one rule
Deep copy event bus
线性DP AcWing 898. 数字三角形
浏览器存储方案
Hash table acwing 840 Simulated hash table
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
3 A VTT端接 稳压器 NCP51200MNTXG资料
[ybtoj advanced training guidance] judgment overflow [error]
Mui WebView down refresh pull-up load implementation