Understand async and await in seconds

One, async definition, await definition:

1. async function: used to define an asynchronous function that returns an AsyncFunction object. An asynchronous function refers to a function that is executed one step at a time through a time loop, and it returns its result through an implicit promise.

2. await: The operator is used to wait for a Promise object. It can only be used in async function.

2. Grammar:

1、async function

async function name([param[, param[, ... param]]]) {
    
    statements}

Note: [param[, param[, ... param]]]is the parameter to be passed, statementsis the function body

2、await

[return_value] = await expression;

Note: await is followed by an expression expression, which is a promise object or any value to wait for.

Three, the return value:

1. The return value of async function: is a promise object, and the result of the promise object is determined by the return value of the async function.

2. await: The expression on the right side of await is generally a promise object, but it can also be other values.

  • If the expression is a promise object, await returns the value of promise success.
  • If the expression is another value, directly use this value as the return value of await.

Four, description

async function:

1. An async asynchronous function can contain zero or more await instructions. The function will suspend the execution of the asynchronous function, wait for the Promise to execute, and then continue to execute the asynchronous function and return the result. The await keyword is valid only in asynchronous functions. The async function usually returns a promise object. If the return value of the async function is not a promise object in the standard sense, it must be wrapped in a promise object.
E.g:

async function foo(){
    
    
	return 1;
}
//等价于

function foo(){
    
    
	return Promise.resolve(1);
}

2. The body of the async function can be considered to be divided by zero or more await expressions. The code between the first line of code and the first await expression runs synchronously. In this case (there is no await expression in the function body), the async function will run synchronously. If there is an await expression in the method body, however, the async method will execute asynchronously.

E.g:

async function foo(){
    
    
	await 1;
}
//等价于
function foo(){
    
    
	return Promise.resolve.then(() => undefined);
}

await: The
await expression will suspend the execution of the current async function and wait for the completion of the Promise processing. If the promise of await fails, an exception will be thrown, which needs to be caught and processed by try-catch.

Fourth, the code:

		async function fn1(){
    
    
            //throw 2
            return Promise.resolve(3);
        }
        const result = fn1();
        result.then(
            value => {
    
    
                console.log('onResolve()',value);
            },
            reason => {
    
    
                console.log('onReject()',reason);
            }
        )
        function fn2() {
    
    
            return new Promise((resolve,reject) => {
    
    
                setTimeout(()=>{
    
    
                    reject(3);
                },1000);
            })
        }

        function fn4(){
    
    
            return 6;
        }
        
        async function fn3(){
    
    
        
            const value = await fn1(); 

        //    const value = await fn2(); //await 右侧表达式为promise,得到的结果就是promise成功的value
            
        //    const value = await fn4(); //await 右侧表达式不是promise,得到的结果就是表达式的值
            
            try{
    
    
                const value = await fn1();
                console.log('value',value);
            }catch(error){
    
    
                console.log("得到失败的结果",error);
            }
            
        }
        fn3();

Content reference: MDN: https://developer.mozilla.org/zh-CN/

Guess you like

Origin blog.csdn.net/weixin_43690348/article/details/107772980