// 第一组
Promise.resolve().then(() => {
console.log(0);
return Promise.resolve(4);
}).then((res) => {
console.log(res)
})
// 第二组
Promise.resolve().then(() => {
console.log(1);
}).then(() => {
console.log(2);
}).then(() => {
console.log(3);
}).then(() => {
console.log(5);
}).then(() =>{
console.log(6);
})
// 1 2 3 4 5 6
- 第一轮
// 一
Promise.resolve()
// 二
Promise.resolve()
第一组代码和第二组代码分别执行对应Promise的resolve()方法。
- 第二轮
// 一
console.log(0);
return Promise.resolve(4);
// 二
console.log(1)
第二轮执行then()方法, 于是第一组代码打印0,返回Promise.resolve(4)的句柄 第二组代码打印1
- 第三轮
// 一
Promise.resolve(4)
// 二
console.log(2)
第三轮中,第一组代码执行第二轮中返回的Promise.resolve(4) 第二组代码继续执行下个then,打印2
- 第四轮
// 一
// res = 4
PromiseObj.then((res)=>{
console.log(res);
})
// 二
console.log(3)
第四轮 第一组代码中在第三轮中的Promise.resolve(4)返回了一个fulfilled值为4的Promise对象,于是执行对应的then方法语句 而第二组代码则是打印3
- 第五轮
// 一
console.log(4);
// 二
console.log(5);
第四轮中,传入then方法的res为4,于是第五轮执行console.log(4)打印输出4,至此第一组代码全部执行完毕 第二组代码打印输出5
- 第六轮
// 二
console.log(6)
第二组代码打印输出6