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);
});
let res = Promise.resolve();
console.log(res);
浏览器答案是0 1 2 3 4 5 6
为什么啊为什么 为什么不是 0 1 4 2 3 5 6 或者 0 1 2 4 3 5 6 为什么
请赐教啊
该回答引用ChatGPT
这是因为,在第一个 Promise.resolve() 中的第一个 then() 里,你返回了一个 Promise 对象,并在第二个 then() 中打印出 Promise 对象 resolve 的值 4。然后,第二个 Promise 中的 then() 依次被调用,按照它们被添加的顺序,打印出 1、2、3、5 和 6。
所以最终的输出顺序是:0 1 2 3 4 5 6。
如果你想要按照你提供的两种不同的顺序输出,请注意 Promise 的返回值和调用顺序。