测试一
setTimeout(() => {
console.log(1)
}, 0);
new Promise((resolve) => { // executor 是同步函数
console.log(2);
resolve();
}).then(() => {
console.log(3);
}).then(() => { // 在3没有执行完的时候 将此回调函数存储在callbacks数组中了,当3执行完将执行此回调函数
console.log(4);
});
console.log(5)
//执行顺序: 2 5 3 4 1
测试二
const first = () => (
new Promise((resolve, reject) => {
console(3);
let p = new Promise((resolve, reject) => {
console.log(7);
setTimeout(() => {
console.log(5);
resolve(6); // ↓下面resolve(1)已经改变了状态,这个就不再生效,因为Promise状态只能改变一次
}, 0);
resolve(1);
})
resolve(2);
p.then((arg) => {
console.log(arg);
})
});
);
first.then((arg) => {
console.log(arg);
});
console.log(4)
// 执行顺序:3 7 4 1 2 5