Front-end-learning-to-organize-notes
Front-end-learning-to-organize-notes copied to clipboard
【挑战系列1】以下代码输出结果
console.log(1);
setTimeout(() => {
console.log(2);
process.nextTick(() => {
console.log(3);
});
new Promise((resolve) => {
console.log(4);
resolve();
}).then(() => {
console.log(5);
});
});
new Promise((resolve) => {
console.log(7);
resolve();
}).then(() => {
console.log(8);
});
process.nextTick(() => {
console.log(6);
});
setTimeout(() => {
console.log(9);
process.nextTick(() => {
console.log(10);
});
new Promise((resolve) => {
console.log(11);
resolve();
}).then(() => {
console.log(12);
});
});
1
7
6
8
2
4
3
5
9
11
10
12
分析:
1. promise resolve之前是同步的,then才是异步的
2. process.nextTick优先于setTimeout、promise.then