gpt4 book ai didi

javascript - Promise `then` 函数不返回任何内容与函数返回另一个 Promise

转载 作者:行者123 更新时间:2023-12-03 10:00:54 25 4
gpt4 key购买 nike

我不明白为什么我们执行这段代码会有如此奇怪的结果。
为什么没有12121212的图像?在每个 1 之后,我们有三个 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); });

最佳答案

这是因为用另一个 promise 解决 promise 涉及多个滴答声。

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

关于javascript - Promise `then` 函数不返回任何内容与函数返回另一个 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59761840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com