如何将Promise.all与其他Promise.all
How to chain a Promise.all with other Promises?
我想按以下顺序执行我的代码:
- 承诺1
- 等待1完成,然后同时执行Promise 2+3
- 最终函数等待Promise 2+3完成
我在弄清楚它时遇到了一些困难,到目前为止我的代码如下。
function getPromise1() {
return new Promise((resolve, reject) => {
// do something async
resolve('myResult');
});
}
function getPromise2() {
return new Promise((resolve, reject) => {
// do something async
resolve('myResult');
});
}
function getPromise3() {
return new Promise((resolve, reject) => {
// do something async
resolve('myResult');
});
}
getPromise1()
.then(
Promise.all([getPromise2(), getPromise3()])
.then() // ???
)
.then(() => console.log('Finished!'));
只返回Promise.all(...
getPromise1().then(() => {
return Promise.all([getPromise2(), getPromise3()]);
}).then((args) => console.log(args)); // result from 2 and 3
我知道这是一个旧线程,但不是
() => {return Promise.all([getPromise2(), getPromise3()]);}
有点多余?胖箭头的想法是,你可以把它写为:
() => Promise.all([getPromise2(), getPromise3()])
这使得生成的代码更加清晰:
getPromise1().then(() => Promise.all([getPromise2(), getPromise3()]))
.then((args) => console.log(args)); // result from 2 and 3
不管怎样,谢谢你的回答,我被这个卡住了:)
相关文章:
- 如何在异步函数中正确使用Promise.all()和then()
- promise.all在一个forEach循环中——所有东西都同时启动
- $q.all当输入数组中的一项不是promise时,Typescript检查器失败
- 当数组中的某个函数没有返回promise时,我可以使用$q.all吗
- 如何将Promise.all与其他Promise.all
- 运行Promise.all()中的When-is-then-then子句
- Promise和Promise.all(数组)在数组实现之前执行
- promise.all只返回第一个数据
- Node.js中的Promise.all没有't调用then函数
- Promise.all()方法没有't解析为一个值
- 如何正确解析和使用promise.all()
- Promise.all() - 在触发多个事件侦听器后执行某些操作
- 为什么 Promise.all(array) 没有立即解决
- Promise.all:解析值的顺序
- RXJS5 vs Promise.all
- 对象文字(哈希)与 Promise.all
- ES6 Promise.all() promise 数组的奇怪分辨率
- Promise.all behavior with RxJS Observables?
- Promise.all() - 如何在不返回 undefined 或 value 的情况下解析()
- 为什么在 Promise.all() 之后没有调用 onJect,其中数组中包含的 Promise.reject() 传