Promise One (resolves in 200ms)
Promise Two (resolves in 400ms)
Promise Three (resolves in 1000ms)
Promise Four (resolves in 300ms)
Promise Five (resolves in 500ms)
这些 promise 将按以下顺序解决
0 - Requests Start
100
200 - Promise One
300 - Promise Four
400 - Promise Two
500 - Promise Five (Most recent application state)
600
700
800
900
1000 - Promise Three (Stagnant application state)
鉴于在我的应用程序中,Promise 的响应决定了应用程序状态,当旧 Promise 的解析速度比新 Promise 慢时,我的应用程序状态将停滞不前。
一个潜在的实现是在前一个请求完成之前不启动下一个请求,但这会大大挂起用户进度。
编辑:
我可能遗漏了一些必要的上下文。我无法判断何时添加 Promise。 Promise.all 启动后无法添加项目,因此 Promise.all 可能不适合我。
对于更正常的用例,所提供的答案可能会很好地工作,但不幸的是我的 API 有点太啰嗦了。
最佳答案
所以您希望所有 promise 真正完成,但仅以最新 promise 的值继续前进?
Promise.all([p1, p2, p3, p4, p5]) // resolves when all promises have resolved
.then((results) => {
// results is an array of resolved values
})
Promise.all() 对你有用吗?请记住,如果任何 promise 被拒绝,它就会被拒绝。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
关于javascript - 给定一组 Promise,我如何强制解决最后一个 Promise 的响应,同时确保所有 Promise 成功解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167970/