32. js - async await and thread sleep

1. Introduction to async await

async await: Let asynchronous code be written like synchronous code

try{}catch(){} can only be used to catch errors in synchronous code, so try{}catch(){} can be used below

async is a modifier for functions, used with functions

<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. Thread sleep

Guess you like

Origin blog.csdn.net/EchoLiner/article/details/131116875