What is the async

Async means “asynchronous”, as the name implies is the key word about asynchronous operations, async is only available in ES7, and we said before Promise, Generator has a great correlation.

Use grammar:

async function name(param){

Param // The name of the argument passed to the function

Statements // The body of the function

}

name().then(function(res){

Res // The result returned by the asynchronous operation

})

The async function returns a Promise object, and you can add callbacks using the THEN method. Specific examples are as follows:

//Promise {<fulfilled>: {... }} show().then(res=>{ console.log("res",res) })

What is await

The await keyword exists in the Async function expression and is used to wait for the Promise object, pause execution, and, after the asynchronous operation is complete, resume execution of the Async function and return the resolved value. If you place await outside the asNYC function, a syntax error will be reported.

Use grammar:

asnyc function name(){

returnValue = await expression;

}

Expression is either a Promise object or a value that needs to be waited for. There are two ways to handle the expression:

With a Promise object, the await blocks the main function execution until the Promise object executes resolve, which returns the value as the result of the operation of the await expression, and then continues down.

For non-Promise objects, these can be strings, booleans, numeric values, and normal functions. Await directly returns the corresponding value instead of waiting for the result of its execution.

Await the Promise object instance as follows:

Async function test1(){console.log(" execute ") return new Promise((resolve,reject)=>{setTimeout(()=>{ Console. Log (" return success after 3 seconds delay ") resolve({a:'1'})},3000)})} async function test2(){let x = await test1() console.log("x",x)//{a: "1"} return x } test2().then(function(res){ console.log("res",res)//{a: "1"} })

Await and normal function instances are as follows:

Function test3(){console.log(" common function ")} async function test4(){await test3()} test4(){await test3()} test4()

Catch exceptions

We know that promises can be in two states, resolved() and Rejected (). What happens if the Promise object becomes rejected?

function testAwait(){ return Promise.reject("error"); } async function test1(){ await testAwait(); console.log("test1"); } test1().then(v=>{console.log(v); }).catch(e=>{ console.log(e); //"error" })

The result of the above instance execution shows that the returned reject state is caught by an outer catch, which then terminates subsequent execution. However, in some cases, even if there is an error, we will continue to execute instead of interrupting. In this case, we use the try… Catch Catches an internal exception.

function test1(){ return new Promise((resolve,reject)=>{ reject("error") }) } async function test2(){ try{ await test1() {} the catch (e) the console. The log (" error ", e)}} test2 (). Then ((res) = > {the console. The log (" implementation success, "res) / / print: Catch (err=>{console.log('err',err)}).

Comparison between Generator and async:

  • Async uses await blocking instead of Generator yield.
  • Compared with Generator, async does not need to run the process function, which perfectly implements asynchronous processes.

From Promise to Generator to Async, the solution for asynchronous programming is becoming more and more perfect, which is the charm of ES6’s ongoing development.