• 41.说说Promise自身的静态方法


    41.说说Promise自身的静态方法

    1. Promise.all (有一个失败就失败,所有的都成功就成功)
    2. Promise.race (有一个成功就成功,有一个失败就失败)
    3. Promise.allSettled (所有的异步操作执行完毕之后,才返回)
    4. Promise.any (只要有一个成功就成功,所有的都失败,才失败)
    5. Promise.resolve (返回一个成功状态的promise对象)
    6. Promise.reject (返回一个失败状态的promise对象)

    验证

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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的状态为失败,其他的状态为成功
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    mysql导入CSV乱码问题解决
    基于有偏距离权值(Weighted cubic O-MOMS with warping)三次O-MOMS插值理论的图像超分辨重构研究-附Matlab程序
    词云的可视化设计教程
    JavaScript快速入门
    javaEE基于springboot民宿推荐系统springmvc+mybatis+jsp]
    基于深度学习网络的疲劳驾驶检测算法matlab仿真
    【面经&八股】大模型方向:面试记录(一)
    双十一“静悄悄”?VR购物拉满沉浸式购物体验
    记录一次扩ubuntu的文件系统的过程
    算法笔记,
  • 原文地址:https://blog.csdn.net/wswq2505655377/article/details/133683525