Promise (9) – Async and await

Time:2020-10-21

Async function

MDN document of Async

1. The return value of async function is promise object
2. The promise result returned by async function is determined by the result of function execution
async function fn1() {
    return 1
}
console.log The output of (fn1()) is of type promise
async function fn1() {
    Return 1 // successful promise with value of 1
    Throw 2 // promise whose reason of failure is 2
    return  Promise.reject (3) // promise with reason 3 failed
    return  Promise.resolve (3) // the successful promise with reason 3
    return new Promise((resolve, reject) => {
        Resolve (4) // successful promise with value of 4
        Reject (4) // the failed promise with reason 4
    }) 
  }

Await expression

The MDN document of await

1. The expression on the right side of await is generally promise object, but it can also be other values
2. If the expression is a promise object, await returns the value of promise success
3. If the expression is any other value, take this value as the return value of await
4. If the promise of await fails, an exception will be thrown, which needs to be caught and handled through try... Catch
5. Await must be written in async function, but there can be no await in async function
function fn2() {  
  return new Promise((resolve, reject) => {  
    resolve(1)  
  })  
}  
  
function fn3() {  
  return 3  
}  
  
function fn4() {  
  return new Promise((resolve, reject) => {  
    reject(2)  
  })  
}  
  
 //There is await in the function body. The function declaration must have async, but there is no await in the async function
(async function(){  
  const r1 = await fn2()  
  console.log (R1) // if the output result is 1, it does not mean that the return value of FN2 is 1, but that fn2() returns a successful promise with a value of 1, and the return value of await is the successful value of promise
  
  const r2 = await fn3()  
  console.log (R2) // the output result is 3. The expression on the right side of await is not promise. This value is returned directly
  
  const r3 = await fn4()  
  console.log (R3) // uncaught exception. If the return on the right side of await is a failed promise, you need to use try... Catch to catch the exception
  try {  
    await fn4()  
  } catch(error) {  
    console.log (error) // the output result is 2
  }
})()
Promise series

Promise from two eyes to light (1) – Preparation
Promise: from blindness to luminescence (2) – promise basis
Promise: from blindness to luminescence (3) – several key issues of promise (1)
Promise from two eyes to double eyes (4) – several key problems of promise (2)
Promise: from blindness to illumination (5) – the overall structure of handwritten promise
Promise from two eyes to two eyes (6) – the constructor of handwritten promise
From “catch” to “two eyes”
Promise from two eyes to double eyes (8) – resolve, reject, all, race method of handwritten promise
Promise (9) – Async and await