当前位置:网站首页>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
边栏推荐
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- 线性DP AcWing 896. 最长上升子序列 II
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- Do you know all the interface test interview questions?
- 深拷貝 事件總線
- JSON serialization and parsing
- [I'm a mound pytorch tutorial] learning notes
- Some sudden program ideas (modular processing)
- FBX import under ue4/ue5 runtime
猜你喜欢

spfa AcWing 851. SPFA finding the shortest path

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

js1day(输入输出语法,数据类型,数据类型转换,var和let区别)

染色法判定二分图 AcWing 860. 染色法判定二分图

移动式布局(流式布局)

堆 AcWing 838. 堆排序

Sweetheart leader: Wang Xinling

JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)

JSON序列化 与 解析

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
随机推荐
Mui WebView down refresh pull-up load implementation
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
一些突然迸发出的程序思想(模块化处理)
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
Bom Dom
In development, why do you find someone who is paid more than you but doesn't write any code?
Leetcode - Sword finger offer 37, 38
VLAN experiment
Use MySQL events to regularly perform post seven world line tasks
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
Interview questions for software testing - a collection of interview questions for large factories in 2022
趣味 面试题
Some sudden program ideas (modular processing)
基于STM32的OLED 屏幕驱动
High performance erasure code coding
Redis sentinel mechanism and configuration
2.6 using recursion and stack - [tower of Hanoi problem]
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
[ybtoj advanced training guidance] judgment overflow [error]
C#运算符