当前位置:网站首页>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
边栏推荐
猜你喜欢

spfa AcWing 852. spfa判断负环

Lekao.com: experience sharing of junior economists and previous candidates in customs clearance

Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)

Heap acwing 838 Heap sort

通过反射执行任意类的任意方法

JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)

趣味 面试题
![JDBC 预防sql注入问题与解决方法[PreparedStatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC 预防sql注入问题与解决方法[PreparedStatement]

High performance erasure code coding

JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
随机推荐
AI mid stage technology research
CPU指令集介绍
Sse/avx instruction set and API of SIMD
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
FBX import under ue4/ue5 runtime
Dijkstra AcWing 850. Dijkstra求最短路 II
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
. Net wechat message template push
Leetcode - Sword finger offer 51 Reverse pairs in an array
Calculate the maximum path sum of binary tree
std::vector批量导入快速去重方法
Is the neural network (pinn) with embedded physical knowledge a pit?
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Simple understanding of ThreadLocal
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Visual studio efficient and practical extension tools and plug-ins
Embedded Software Engineer career planning
模块化 CommonJS ES Module