当前位置:网站首页>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 900. 整数划分
- In development, why do you find someone who is paid more than you but doesn't write any code?
- MySQL and PostgreSQL methods to grab slow SQL
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- Drools executes string rules or executes a rule file
- 染色法判定二分图 AcWing 860. 染色法判定二分图
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- bellman-ford AcWing 853. 有边数限制的最短路
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
猜你喜欢
堆 AcWing 838. 堆排序
2.6 using recursion and stack - [tower of Hanoi problem]
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
浏览器存储方案
Drools dynamically add, modify, and delete rules
High performance erasure code coding
线性DP AcWing 896. 最长上升子序列 II
BOM DOM
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
随机推荐
Bom Dom
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
Some sudden program ideas (modular processing)
Do you know all the interface test interview questions?
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
软件测试面试题-2022年大厂面试题合集
深拷贝 事件总线
Wechat official account payment prompt MCH_ ID parameter format error
Deep Copy Event bus
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
. Net, C # basic knowledge
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
堆 AcWing 839. 模拟堆
. Net wechat message template push
Is the neural network (pinn) with embedded physical knowledge a pit?
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
In development, why do you find someone who is paid more than you but doesn't write any code?
Docsify deploy IIS
线性DP AcWing 899. 编辑距离