promise用法事例

172 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise</title>
</head>
<body>
<script src="js/jquery.js"></script>
<script>
    function aaa(){
        var pro = new Promise(function (res,red){
            $.get('http://182.254.146.100:3000/api/getindexmenu',function(data){
                console.log(111);
                if(true){
                    res();
                }else{
                    red();
                }
            })
        })
        return pro;
    }
    function bbb(){
        var pro = new Promise(function (res,red){
            $.get('http://127.0.0.1:3000/api/getindexmenu',function(data){
                console.log(222);
            })
        })
        return pro;
    }
    aaa().then(
        function(){
            bbb();
        }
    ).catch(function(){
        alert('red');
    })

   /* ajax1().then(
        function(){
            ajax2();
        }
    ).catch(function (){
        console.log('有毛病了');
    })
    var len = 0;
    function ajax1(){
        var pro = new Promise(function(res,red){
            $.get('http://182.254.146.100:3000/api/getindexmenu',function(data){
                len = 1;
                //表示执行then里面的function
               if(true){
                    res();
                }else{
                    red();
                }
            })
        })
        return pro;
    }

    function ajax2(){
        $.get('http://127.0.0.1:3000/api/getindexmenu',function(data){
            console.log(len);
        })
    }*/
</script>
</body>
</html>