Async
- Definition: asynchronous function so written (as a function of the synchronization Generator function syntax sugar)
- Principle: Generator function and automatic actuator spawn packaged in a function
- Form: Generator The function * replace the async , to yield replaced await
- statement
- Named function: the async function Func () {}
- Function expression: FUNC = const the async function () {}
- Arrow functions: FUNC = const the async () => {}
- Object Method: FUNC the async const {obj = () {}}
- Method class: class {the Cla the async Func () {}}
- await command: Wait for the current Promise object state changes is completed
- Normal: followed Promise As a result the object is returned, otherwise it returns the corresponding value
- Subsequently Thenable objects: it is equivalent to the Promise object returns a result
- Error handling: await the command Promise object into the try-catch (which can be put s)
Scenarios
- Order to complete the asynchronous operation
main difficulty
- Async function returns Promise object, using the then () callback is added
- Internal return subsequent return value becomes the then () of the reference
- Internal error causes the return throw Promise object becomes rejected state, by the catch () received
- Return Promise objects must wait until all internal await command Promise object to perform complete state change will occur unless it encounters a return statement or throw an error
- Any await command Promise object becomes rejected state, the entire Async function will interrupt execution
- Hope that even if an asynchronous operation failed before and do not break the back of the asynchronous operation
- Await the command Promise object into the try-catch in the
- await command Promise objects with a the catch ()
- await command Promise object may become rejected state, preferably into the try-catch which in
- More await command Promise objects if there is secondary to the relationship, it is best to let them triggered simultaneously
- await command can only be used in Async function, otherwise it will error
- Array using forEach () performs async / await fail, can be used for-of and Promise.all () instead of
- You can keep running the stack, as a function of the context of the implementation of Async function exists, to complete the implementation of disappeared
Asynchronous iterator ( for the wait-of- ) :, loop waiting for each Promise object becomes resolved before entering the next state