· Async - await syntactic sugar Promise and Generator, the purpose just to make us more fluent when writing code to enhance readability of the code.
· Async - await mechanism is built on top of Promise, and can not take its place
The basic syntax:
the async function demo01 () { the let Result = the await Math.random () the console.log (Result) } demo01 () // output a random number
async:
async is used to represent asynchronous function, the function will return a defined Promise objects can be added using the callback method then
async function demo02 () { return 'return result' } demo02 () the then (RES. => the console.log (RES)) // Output: returns the result (if the async defined function returns a value, corresponding to Promise.resolve ( " return result '))
await: await must appear within the async function can not be used alone.
· Behind await expression can follow any js. Its primary intent is to wait for the state Promise object is resolved.
If await is the object Promise cause asynchronous function 停止
execution and 等待 Promise
settlement, if such is the normal expression is immediately executed .
Use :
function SLEEP (SECOND) { return new new Promise ((Resolve, Reject) => { the setTimeout (() => { Resolve ( 'enough SLEEP ~' ) }, SECOND) }) } the async function dome03 () { the await () = > {console.log ( 'expression immediate execution' )} the let Result = the await sleep (2000 ) the console.log (Result) // need to wait until the sleep function execution completes resolve the output (output after 2000 milliseconds: enough sleep ~) }
Example 1 (simulation when a request to return to rely on a parameter request and await the async Example):
// analog asynchronous request function SLEEP (SECOND, param) { return new new Promise ((Resolve, Reject) => { the setTimeout (() => { Resolve (param); }, SECOND); }) } the async function Test () { the let RESULT1 = await SLEEP (2000, 'req01' ) // wait for the first implementation of the completion await the let result2 await SLEEP = (1000, 'req02' + RESULT1) // wait for a second await execution completion let result3 = await sleep (500, 'req03' + result2) // wait for completion of all await execution the console.log ( `$ --- $ {} {result3 result2} --- $ {}` RESULT1) } Test () // wait for all the output await execution completion: req01 --- req02req01 --- req03req02req01
Example 2 (when needed and requests a plurality of asynchronous transmission requests are independent instances):
function SLEEP (SECOND) { return new new Promise ((Resolve, Reject) => { the setTimeout (() => { Resolve ( 'Request DONE!' + Math.random ()); }, SECOND); }) } // correct wording the async function correctDemo () { the let P1 = SLEEP (1000 ); the let P2 = SLEEP (1000 ); the let P3 = SLEEP (1000 ); // Promise.all ( 'an object iteration') a plurality of Promise instance, packaged into a new instance Promise, Promise of n-time processed objects. await Promise.all ([p1, p2, p3]); the console.log ( '~ Clear The loading' ); } // Error writing: synchronization request can not be achieved along the following lines, one must wait for the first sleep to await receiving solution will perform the second await the async function bugDemo () { await sleep ( 1000 ) the await SLEEP ( 1000 ) the await SLEEP ( 1000 ) the console.log ( '~ Clear The loading' ) } correctDemo () bugDemo ()
Error handling:
Processing error
function SLEEP (SECOND) { return new new Promise ((Resolve, Reject) => { the setTimeout (() => { Reject ( 'want to SLEEP ~' ); }, SECOND); }) } the async function errorDemoSuper () { the try { the let result = await SLEEP (1000 ); the console.log (result); } the catch (ERR) { the console.log (ERR); // performed when waiting for the result await Reject } } errorDemoSuper () // output: want to sleep ~
Catching errors:
// definition of a method of throwing an exception function ErrorTest (SECOND) { the throw new new Error ( 'throws an exception!' ) }
Mode 1: The try ... catch capture
try { errorTest() }catch(error){ console.log(error) }
Mode 2: Capture the then callback
errorTest().then( resolve => console.log(resolve), error => console.log(error) )
Mode 3: caught in the catch Promise
errorTest().catch( error => console.log(error) )
There is always an excuse to want to escape, you want to always have a method for success! ! !