<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    let a = 1

    let promise = new Promise(function(resolve,reject){
        if(a == 10){
            resolve('成功')
        }else{
            reject('失败鸟')
        }
    })

    //promise.then(success,fail);
    promise.then(x => {
        console.log(x);
    },err => {
        console.log(err)
    })

    promise.catch(err =>{ //发送错误,别名 
        console.log(err)
    })

    //成功的简写方法
    let p1 = Promise.resolve('aaa');
    p1.then(res => {
        console.log(res)
    })

    //失败的简写方法
    let p2 = Promise.reject('aaa');
    p2.then(res => {
        console.log(res)
    }).catch(err => {
        console.log("错误了")
    })

    
    
    let p3 = Promise.resolve('aaa');
    p3.then(res => {
        console.log(res)
    })
    let p4 = Promise.resolve('bbb');
    p4.then(res => {
        console.log(res)
    })
    let p5 = Promise.resolve('ccc');
    p5.then(res => {
        console.log(res)
    })
    //必须保证 promise对象都是resolve状态(成功状态)
    Promise.all([p3,p4,p5]).then(res=>{
        let [res1,res2,res3] = res
        console.log(res1,res2,res3)
    })

    // race对象只要有有一个resolve状态(成功状态)
    Promise.race([p3,p4,p5]).then(res=>{
        let [res1,res2,res3] = res
        console.log(res1,res2,res3)
    })


</script>

本文由 吾爱 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论