JavaScript是单线程指的是同一时间只能干一件事情,
所以想看到Promise执行前后的对比可以通过定时器完成。
Promise
Promise.then
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('成功');
}, 10000);
})
const p2 = p1.then(() => {
console.log('Promise执行resolve后');
console.log(p1);
console.log(p2);
}, () => {})
setTimeout(() => {
console.log('Promise.then执行完成');
console.log(p1);
console.log(p2);
}, 10001);
console.log('Promise执行resolve前');
console.log(p1);
console.log(p2);
展开图