此文作废,事件循环参考最新博文
https://blog.csdn.net/s8806479/article/details/131270201
———————————————————2302.12.4分割线———————————————————————
https://www.jianshu.com/p/d3ee32538b53
一个事件循环有一个或者多个任务队列;
每个事件循环都有一个microtask队列
macrotask队列就是我们常说的任务队列,microtask队列不是任务队列
一个任务可以被放入到macrotask队列,也可以放入microtask队列
当一个任务被放入microtask或者macrotask队列后,准备工作就已经结束,这时候可以开始执行任务了。
开始 -> 取task queue第一个task执行 -> 取microtask全部任务依次执行 -> 取task queue下一个任务执行 -> 再次取出microtask全部任务执行 -> …
function func(num) {
return function () {
console.log(num)
};
}
setTimeout(func(1));
async function async3() {
await async4();
console.log(8)
//等同于
//Promise.resolve(async4()).then(() => {
// console.log(8);
//})
}
async function async4() {
console.log(5)
}
async3();
function func2() {
console.log(2);
async function async1() {
await async2();
console.log(9)
//等同于
//Promise.resolve(async2()).then(() => {
// console.log(9);
//})
}
async function async2() {
console.log(5)
}
async1();
setTimeout(func(4))
}
setTimeout(func2);
setTimeout(func(3));
new Promise(resolve => {
console.log('Promise');
resolve()
}).then(() => console.log(6))
.then(() => console.log(7));
console.log(0);
有个疑问
function func(num) {
return function () {
console.log(num)
};
}
function func2() {
setTimeout(func(4))
}
setTimeout(func2);
setTimeout(func(3));
输出3 4
我认为是这样的,微队列一开始setTimeout(func2); setTimeout(func(3));这么个顺序,然后进第一个,好家伙里面还有setTime那么微队列变成setTimeout(func(3));setTimeout(func(4))
function func(num) {
return function () {
console.log(num)
};
}
setTimeout(func(4))
setTimeout(func(3));
输出4 3
参考了以下文章
https://blog.csdn.net/qq_40677590/article/details/107128860
const first = () => (new Promise((resolve, reject) => {
console.log(1)
let p = new Promise((resolve, reject) => {
console.log(2)
setTimeout(() => {
console.log(3)
resolve(4)
})
resolve(5)
})
resolve(6)
p.then((arg) => {
console.log(arg)
})
}))
first().then((arg) => {
console.log(arg)
})
console.log(7)
/*
同步执行:1 2 7
微任务:5 6
宏任务:3
*/
//1 2 7 5 6 3
setTimeout(() => {
console.log(1)
})
new Promise((resolve, reject) => {
console.log(2)
resolve()
}).then(() => {
console.log(3)
new Promise((resolve, reject) => {
console.log(4)
resolve()
}).then(() => {
console.log(5)
}).then(() => {
console.log(6)
})
}).then(() => {
console.log(7)
})
new Promise((resolve, reject) => {
console.log(8)
resolve()
}).then(() => {
console.log(9)
})
/*
同步执行:2 8
微任务:3 [同:4] 9 5 7 6
宏任务:1
*/
//2 8 3 4 9 5 7 6 1
console.log('script start');
async function async1() {
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2 end');
}
async1();
setTimeout(function() {
console.log('setTimeout');
}, 0);
new Promise(resolve => {
console.log('Promise');
resolve();
}).then(function() {
console.log('promise end');
});
console.log('script end');
console.log('script start');
async function async1() {
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2 end');
return Promise.resolve()
.then(()=>{
console.log('async2 end in promise')
})
}
async1();
setTimeout(function() {
console.log('setTimeout');
}, 0);
new Promise(resolve => {
console.log('Promise');
resolve();
}).then(function() {
console.log('promise end');
});
console.log('script end');
https://zhuanlan.zhihu.com/p/74339572
https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/7