Promise.all 一个失败及失败,都成功则成功
/* 1. Promise.all 一个失败及失败,都成功则成功 */
var a1 = Promise.resolve(1)
var a2 = Promise.resolve(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.all([a1, a2, a3, a4]))
// [1,2,3,4]
var a1 = Promise.resolve(1)
var a2 = Promise.reject(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.all([a1, a2, a3, a4]))
// 报错 返回 2
2. Promise.race 一个状态变化,就立刻返回
/* 2. Promise.race 一个状态变化,就立刻返回*/
var a1 = Promise.resolve(1)
var a2 = Promise.resolve(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.race([a1, a2, a3, a4]))
// 1
var a1 = Promise.reject(2)
var a2 = Promise.reject(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.race([a1, a2, a3, a4]))
// 2
3. Promise.allSettled 状态都变化完毕后,再返回
/* 3. Promise.allSettled 状态都变化完毕后,再返回 */
var a1 = Promise.resolve(1)
var a2 = Promise.resolve(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.allSettled([a1, a2, a3, a4]))
// [1,2,3,4]
var a1 = Promise.resolve(1)
var a2 = Promise.reject(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.allSettled([a1, a2, a3, a4]))
// [1,2,3,4] // 其中reject的状态为失败,其他的状态为成功
4. Promise.any 一个成功及成功,都失败则失败,和all相反
/* 4. Promise.any 一个成功及成功,都失败则失败,和all相反 */
var a1 = Promise.resolve(1)
var a2 = Promise.resolve(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.any([a1, a2, a3, a4]))
// 1
var a1 = Promise.reject(1)
var a2 = Promise.reject(2)
var a3 = Promise.resolve(3)
var a4 = Promise.resolve(4)
console.log(Promise.any([a1, a2, a3, a4]))
// 2
var a1 = Promise.reject(1)
var a2 = Promise.reject(2)
var a3 = Promise.reject(3)
var a4 = Promise.reject(4)
console.log(Promise.any([a1, a2, a3, a4]))
// 失败状态 AggregateError: All promises were rejected