JS async

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script >
        //函数回调
            function Display(some){
                console.log(some);
            }
            function Add(num1,num2,callback){
                var sum=num1+num2;
                callback(sum);
            }
            Add(6,3,Display);
        //函数异步
            //超时执行  3000ms后执行
            setTimeout(function() {Display("string")}, 3000);
            //等待间隔 每1.5s执行一次
            setInterval(function() {Display("string")}, 1500);
        //Promise对象
            let myPro=new Promise(function(myResolve,myReject){
                var x=0;
                if(x==0){
                    myResolve("ok");
                }else{
                    myReject("error");
                }
            });
            
            myPro.then(
                //成功时
                function(value){
                    console.log(value);
                },
                function(error){
                    console.log(error);
                }
            )
        //Async 返回promise
            async function myFun(){
                return "asycn";
            }
            myFun().then(
                function(value){
                    console.log(value);
                },
                function(error){
                    console.log(error);
                }
            )
        //Await 只能在async函数中使用
            async function myFun1(){
               let myPromise = new Promise(function(myResolve, myReject) {
                 myResolve("I love You !!");
               });
               let arg= await myPromise;
               console.log(arg);
            }
            myFun1();
            
            
        </script>
    </body>
</html>

 

posted @ 2022-03-16 18:21  lwx_R  阅读(224)  评论(0)    收藏  举报