Async: Mainly solves the problem of deep internal nesting

Async function fn () {let result = await XXX; }}Copy the code

The characteristics of the async

  1. Async can only be placed in async functions
  2. More semantic than Generator (syntactic sugar)
  3. Await can be a promise object, a number, a string, a Boolean
  4. The async function returns a Promise object
async function fn () {
	return 'welcome'
}
fn().then(res= > {
	console.log(res); // welcome
})
Copy the code
  1. As soon as the Promise state changes to reject after the await statement, the entire async function is interrupted.
async function fn () {
	throw new Error('Error ~~~');
}
fn().then(res= > {
	console.log(res);
}, err= > {
	console.log(err); / / go to the error
})
Copy the code