星世界
首页 > javascript > await的使用
编者:张叶星
发布时间:2020-07-21 09:36:49
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>await的使用</title>
    </head>
    <body>
        <script type="text/javascript">
            function who() {
                return new Promise(resolve => {
                    setTimeout(() => {
                        resolve('>');
                    }, 200);
                });
            }

            function what() {
                return new Promise(resolve => {
                    setTimeout(() => {
                        resolve('lurks');
                    }, 1600);
                });
            }

            function where() {
                return new Promise(resolve => {
                    setTimeout(() => {
                        resolve('in the shadows');
                    }, 500);
                });
            }

            async function msg() {
                // a, b, c 挨个执行
                const a = await who();
                console.log(`${ a }`);
                const b = await what();
                console.log(`${ b }`);
                const c = await where();
                console.log(`${ c }`);

                // 会等待所有的异步调用都执行完成后执行
                console.log(`${ a } ${ b } ${ c }`);
                
                // >
                // lurks
                // in the shadows
                // > lurks in the shadows
            }

            msg();
        </script>
    </body>
</html>

image.png



本文地址:
转载请著名出处,谢谢!
欢迎交流
QQ:419268793
编者:张叶星
发布时间:2020-07-21 09:36:49
用户名:
密码:
      本站的部分文章和图片来自互联网,特别鸣谢 “百度图片”、“笑话集 www.jokeji.cn ”等, 如果本站有某些文章或图片侵犯了您的权益,麻烦您告诉我,我会及时处理。谢谢!微笑
QQ:419268793