当前位置:网站首页>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
边栏推荐
- Fluent fluent library encapsulation
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- JSON serialization and parsing
- Input box assembly of the shutter package
- Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
- JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
- 通过反射执行任意类的任意方法
- 堆 AcWing 839. 模拟堆
- 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)
猜你喜欢

spfa AcWing 851. spfa求最短路

C#修饰符

Go learning notes - multithreading

分布式机器学习框架与高维实时推荐系统

JSON serialization and parsing

线性DP AcWing 898. 数字三角形

The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night

Rust search server, rust quick service finding tutorial

Redis sentinel mechanism and configuration

js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
随机推荐
百款拿来就能用的网页特效,不来看看吗?
Is the neural network (pinn) with embedded physical knowledge a pit?
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
China traffic sign detection data set
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Openssh remote enumeration username vulnerability (cve-2018-15473)
Go learning notes - go based interprocess communication
Distributed machine learning framework and high-dimensional real-time recommendation system
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Go learning notes - multithreading
Do you know all the interface test interview questions?
Redis introduction, scenario and data type
. Net wechat message template push
Some sudden program ideas (modular processing)
包管理工具
Shutter encapsulated button
[ybtoj advanced training guidance] cross the river [BFS]
防抖 节流
线性DP AcWing 897. 最长公共子序列