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

标签 javascript promise

我不明白为什么我们执行这段代码会有如此奇怪的结果。
为什么没有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/

相关文章:

javascript - 使用 Promise 等待函数完成

javascript - 将用户输入存储为 var

javascript - 为什么在将 `maxlength` 属性设置为 `input` 时不能使用点表示法?

Javascript - 带 _blank 的位置 href

javascript - Promise Resolve 返回 2 个数组,其中一个未定义

javascript - NodeJS 中的递归异步循环

javascript - 根据属性中指定的大小动态更改图像

javascript - 无法处理 x-editable 弹出字段中的验证属性

javascript - 使用 sinon 和 bluebird 对 Promisified 函数进行 Stub

angularjs - Angular $http : setting a promise on the 'timeout' config