32、js - async await 与 线程休眠

1、 async await简介

async await:让异步代码写起来像同步代码一样

try{}catch(){}只能用于捕获同步代码的错误,所以下面可以使用try{}catch(){}

async是函数的修饰符,与函数一起使用

<script src="./axios.min.js"></script>
<script>

    async function getData() {

        try {

            const p1 = await axios({ url: "http://hmajax.itheima.net/api/province" });

            console.log(p1);

            const p2 = await axios({ url: "http://hmajax.itheima.net/api/province" });

            console.log(p2);

            const p3 = await axios({ url: "http://hmajax.itheima.net/api/province" });

            console.log(p3);

        } catch (error) {
            // 捕获 async await 的异常
            // try 里面的请求发生错误都会在这里进行捕获
            console.log(error);
        }

    }
    getData();

</script>

2、线程休眠

猜你喜欢

转载自blog.csdn.net/EchoLiner/article/details/131116875
今日推荐