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

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.

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.