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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.