Async/Await

1. The async/await usage:
  1. Async, which precedes functions as a keyword so that ordinary functions become asynchronous
  2. Asynchronous async function calls are used in the same way as ordinary functions
  3. The async function returns a Promise object
  4. Async functions used with the await keyword (blocking code down) are asynchronous methods, but blocking
2. Async /await usage scenario

1. Async mainly processes asynchronous operations

Requirement: Perform the first step and return the results of the first step to the second step for use. In Ajax, we get the data returned from an interface first, and then use the data returned from the first step to perform the interface call of the second step, so as to achieve asynchronous operationCopy the code
3. Async basic usage
// Basic async functionslet asyncFun = async function() {return1} console.log(asyncFun()) // returns a Promise objectCopy the code
4. Async function syntax

1. The callback function specified by the THEN method is executed only after the asynchronous operation inside the async function is completed

2. Async functions can use await inside

The return value is a Promise object

async functionName ([param[, param[,... param]]]) {statements} name: function name. Param: The name of the argument to be passed to the function statements: function body statements. Return value: The returned Promise object will be asyncfunctionIs parsed or rejected with an exception thrown by the function.Copy the code
5. Await the grammar

1. Await can only be used inside async functions, normal functions will get an error if used

2. Await is placed before a Promise call, and await forces the code to wait until the Promise object resolve returns the value of resolve as the result of the await expression

[return_value] = await expression; Expression: A Promise object or any value to wait for. Return value: Returns the result of processing the Promise object. If you are waiting for something other than a Promise object, the value itself is returned.Copy the code
6. Conclusion:
  1. An async function returns a Promise object. When the function executes, it returns an await object, waits until the triggered asynchronous operation is complete, and then executes the following statement in the function body.
  2. The await keyword must be inside the async function
  3. We need a promise object after the await keyword (if not, we call resolve to transform it)
  4. The return result of the await keyword is the result of the Promise execution after 7, which could be resolved or Rejected