当前位置:   article > 正文

async await 面试题 实践中会遇到的_asyn面试题

asyn面试题
<!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>
    <script>
        async function async1() {
            console.log('async1 start')
            await async2()
            console.log('async1 end')
        }
        function async2() { // 去掉了 async 关键字
            console.log('async2');
        }
        console.log('script start')
        setTimeout(function () {
            console.log('setTimeout')
        }, 0)
        async1();
        new Promise(function (resolve) {
            console.log('promise1')
            resolve();
        }).then(function () {
            console.log('promise2')
        })
        console.log('script end')

         /* 
         
            script start
            async1 start
            async2
            promise1
            script end
            async1 end
            promise2
            setTimeout
         
          */
/* 
        async 
        调用async函数时 会返回一个promise对象;
        当这个async函数返回一个值时;
        promise的resolve方法会负责传递这个值
        当这个async函数抛出异常;
        promise的reject方法会负责传递这个异常值

        async函数中可能会有await表达式
        这会使async函数暂停执行
        等待promise的结果出来 
        然后恢复async函数的执行并返回解析值 resolved;

        遇到await表达式时 会让async函数暂停执行
        等到await后面的语句状态发生改变之后 再恢复
        async函数的执行 并返回解析值;

        
         */
        async function fun1(){
            console.log("async1")
        }
        async function fun2(){
            console.log("async2")
        }
        function fun3(){
            console.log("333")
        }
        fun1()
        console.log( fun1() )
        fun2()
        fun3()
        console.log( fun3() )
    </script>
</body>

</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/555102
推荐阅读
相关标签