Skip to main content

What is async and await?

Both async and await are syntaxes provided in es7. Compared with the promise of es6, this method has higher code readability. Literally understand that async means asynchronous, await means waiting, then their role is very obvious: async: declare that a function is asynchronous; await: wait for an asynchronous function to complete execution.

async function:

  • The return value of the function is a promise object.

  • The result of the promise object is determined by the return value of the async function execution.

await expression:

  • The expression on the right side of await is usually a promise object, but it can be other values.

  • If the expression is a promise object, await returns the value of the promise's success.

  • If the expression is any other value, directly use this value as the return value of await.
  • await must be written in the async function, but there can be no await in the async function.

  • If the promise of await fails, an exception will be thrown, which needs to be caught and handled by try...catch.

See the code below:

    <script>
        async function fun(){
            let p=new Promise((resolve,reject)=>{
                // resolve("OK");
                reject("err66")
            })
            try{
                let a=await p;
                // console.log(a)
            }
            catch(e){
                console.log(e)
            }
        }
        fun()
    </script>

The async syntax has advantages when dealing with then chains. If we use the then nesting of promises, we may write multiple then chains. The combination of the two can solve the problem of callback hell. The following is a multi-interface call using async and await. Code:

     async function fun1() {
        try{
            console.log("fun1...start")
        let l1 = await fun2();
        let l2 = await fun3();
        console.log(l1,l2);
        }catch(e){
            console.log(e);
        }
    }
    function fun2() {
         return "222";
    }  
    function fun3() {
        return Promise.resolve("333");
    }
    fun1();
        console.log("main---1");
        console.log("main---2");
        console.log("main---3");
        console.log("main---4");
        console.log("main---5");
        //fun1...start
        // main---1
        // main---2
        // main---3
        // main---4
        // main---5
        // 222 333

The await expression will block the code behind await, execute the synchronous code outside async first, wait for the synchronous code to finish executing, and then return to the inside of async to continue execution.





Comments

Popular posts from this blog

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.