通过一个函数返回一个数字与一个函数返回另一个promise的promise

问题描述 投票:1回答:1

我不明白为什么我们执行此代码会有如此奇怪的结果。为什么没有12121212的图像?在每个1之后,我们有3个2。

Promise.resolve()
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); })
.then(() => { console.log(1); return Promise.resolve(); });

Promise.resolve()
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2))
.then(() => console.log(2));
javascript promise
1个回答
2
投票

这是因为在解决另一个承诺时涉及多个滴答声。

但是,实际上,您应该忽略这一点,永远不要依赖独立的诺言链之间的时间安排。

© www.soinside.com 2019 - 2024. All rights reserved.