Promise.all
-
所以Promise都返回,或者出现reject才返回
-
值得注意的是,返回的数组结果顺序与传入的promise数组的顺序一致
-
Promise.all成功返回数组,
-
失败返回失败数据,一但失败就不会继续往下走
let p1 = Promise.resolve('aaa') let p2 = Promise.resolve('bbb') let p3 = Promise.reject('ccc') let p4 = Promise.resolve('ddd') Promise.all([p1, p2, p3, p4]).then(res => { console.log(res); //返回数组 }).catch(err => { console.log(err); })
Promise.race
-
Promise.race是赛跑的意思,也就是说Promise.race([p1, p2, p3])里面的结果哪个获取的快,就返回哪个结果,不管结果本身是成功还是失败
let p1 = Promise.reject('aaa') let p2 = Promise.resolve('bbb') let p3 = Promise.reject('ccc') let p4 = Promise.resolve('ddd') Promise.race([p1, p2, p3, p4]).then(res => { console.log('成功', res); //返回数组 }).catch(err => { console.log('失败', err); })