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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.