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

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.