async函数相关

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 第一点:async函数与普通函数区别,及初步认识async函数
        function normal(data) {
            console.log(data);
            return 123
        }
        console.log(normal(1));  // 返回的是return值
        async function foo(data) {
            console.log(data);
            return 123
        }
        console.log(foo(11));  // 返回的是promise对象

        /**
         * 首先async函数是什么?
         * --->简单说它就是Generator的语法糖,语法糖: 带有一定功能的关键字.
         * 功能:创建并返回一个promise实例,且async只要执行就会返回promise实例对象,用于解决异步程序产生的bug
        */
        // 下面是async函数的几种写法
        var obj = {
            async bar() {
                console.log('1111111111111111111');
                return 123
            },
            async bar1() {

            },
            foo: async () => {

            },
            test: async function () {

            }
        }

        // 第二点:对于async函数return值的学习
        // 现在我们已经知道了普通函数返回的是return值,而async函数返回的是promise对象,那么async函数的return值去哪里了呢?
        async function foo(data) {
            console.log(data);
            return 123
        }
        console.log(foo(11));
        /*
        // 经打印在chrome中async函数的return值会被保存在[[PromiseResult]]中
        在火狐浏览器中则会保存在[[PromiseValue]]中,可以看出async的return值并不是消失了,而是以此种方法保存起来
        Promise
        [[Prototype]]: Promise
        [[PromiseState]]: "fulfilled"
        [[PromiseResult]]: 123
        */

        // 关于await
        /**
         * await  注意
         * 1: await 需要在async中写;
         * 2: await 后是 promise实例  
         * 3: await 作用 获取  promise.[[PromiseValue]] 的值
         * 
         * [[PromiseValue]] 哪些能赋值:
         * 1: async函数的 return
         * 2: new promise中 resolve实参
         * 3: then 中return  (catch finally 中retrun)
         * 4: Promise.resolve() 实参  Promise.reject() 实参
         * 
         */
        (async function () {

            console.log('run run run');
            // 第二种接收 函数 foo 返回值的方式 是  await 
            let res = await foo();
            console.log(res);
        }())

        async function foo(data) {
            console.log(data);
            return 123
        }

        // await[[PromiseValue]]的取值的
        (async () => {
            // resole 实参去哪了  ---->  p.[[promiseValue]]
            let p = new Promise((resolve, reject) => {
                resolve(123)
            });
            console.log(p);
            //    Promise.resove() 实参去哪了 ----->  p1.[[promiseValue]]
            let p1 = Promise.resolve('234');
            console.log(p1);
            // then-callback 的retrun去哪里了 ----> p2.[[promiseValue]]
            let p2 = p1.then(res => 456);
            console.log(p2);

            // foo的return 的中去哪的?   ------> p3.[[promiseValue]]
            async function foo() {
                return 789
            };

            let p3 = foo()
            console.log(p3);

            // res作用接受 await 的返回子
            let res = await p;
            console.log(res);
            let res1 = await p1;
            console.log(res1);
            let res2 = await p2;
            console.log(res2);
            let res3 = await p3;
            console.log(res3);
            // 综上所诉
            // 语法: await promise实例
            // 作用:  获取 promise实例.[[PromiseValue]] 的值

            // [[promiseValue]] 赋值 查询有哪些方式
            //  [[promiseState]]:fufiled状态赋值:赋值:
            // resolve() 实参 
            // then-callback return 
            // catch-callback return
            // finally-callback return
            // Promise.resolve() 实参

            // [promiseState]]:fufiled状态赋值:读取值;
            // then-callback 形参
            // await 返回值
        })()

            // 同步写法异步程序
            (async () => {
                let p = new Promise((resole, reject) => {
                    setTimeout(() => {
                        resole(1111)
                    }, 1000)
                })
                let p1 = new Promise((resole, reject) => {
                    setTimeout(() => {
                        resole(222)
                    }, 500)
                })
                let p2 = new Promise((resole, reject) => {
                    setTimeout(() => {
                        resole(333)
                    }, 1000)
                })

                // 同步写法... 实际上异步程序
                console.log('同步1111111111111111');
                // 等 await 执行结束 执行下一个 await 
                // -----> 解决异步问题导致程序运行bug 
                let res = await p;
                console.log(res);
                let res1 = await p1;
                console.log(res1);
                let res2 = await p2;
                console.log(res2);
                console.log('222222222222222');

                // console.log('同步1111111111111111');
                // p.then(res=>{
                //     console.log(res);
                // })
                // p1.then(res=>{
                //     console.log(res);
                // })
                // p2.then(res=>{
                //     console.log(res);
                // })
                // console.log('222222222222222');

            })()
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值