【发布时间】:2018-04-16 01:23:44
【问题描述】:
有没有办法保证Promise.all 在内部承诺的then 链之后解析?
例子:
const promiseOne = new Promise((resolve, reject) => {
setTimeout(function(){
console.log('promiseOne after 1 second')
resolve()
}, 1000)
}).then(()=> {
setTimeout(function(){
console.log('promiseOne then chain, after 2 seconds')
}, 1000)
})
Promise.all([promiseOne])
.then(() => {
console.log('Promise.all then chain after 1 second')
})
日志:
promiseOne after 1 second
Promise.all then chain after 1 second
promiseOne then chain, after 2 seconds
【问题讨论】:
-
在“promiseOne then chain, after 2 seconds”回调中返回另一个在 1 秒后解决的承诺。现在你的回调返回未定义。并立即解决。
-
promise.all 就是这样工作的,无论如何将它用于单个 Promise 是多余的
标签: javascript promise