This is a big pity (failure). This is a pity (failure).

  1. resolveWhen the function is executed, thepromiseThe state of the from pendingtofulfilledsuccessful
  2. rejectWhen the function is executed, thepromiseThe state of the frompendingtorejectedfailure

Promise.reject()

new Promise((resolve, reject) => {
	reject()
})
Copy the code

Promise.resolve()

new Promise((resolve, reject) => {
	resolve()
})
Copy the code

Promise.all([promise1, promise2, promise3]) waits after all promises have been fulfilled and can be used to handle concurrent tasks

/ / behind. Then the configuration function, is in front of all promise after the completion of execution, can be used to handle concurrent tasks promise. All ([promise1 promise2, ((values) => {// values is an array of values[0] => success of the promise1})Copy the code

Promise.race([promise1, promise2, promise3])

What is async/await?

The async functions added to the ES7 standard are syntactic sugar for Generator functions from the current internal implementation.

It is Promise based and compatible with all existing Promise based apis.

Async keyword

  1. asyncThe keyword is used to declare an asynchronous function (e.gasync function asyncTask1() {... })
  2. asyncThe regular function is automatically converted to a Promise, and the return value is also a Promise object
  3. asyncFunctions can be used internallyawait

The await keyword

  1. awaitWait for the asynchronous function to completevar result = await someAsyncCall()
  2. awaitPlaced before a Promise call, it forces the rest of the code in the async function to wait until the Promise completes and returns the result
  3. awaitCan only be used with Promises
  4. awaitOnly in theasyncInternal use of functions

What are the advantages of async/await over Promise?

  1. Synchronous code reading experience (Promise gets rid of callback hell, but then chain-call reading burden still exists)
  2. More consistent error handling with synchronous code (async/await can be handled with mature try/catch, which is cleaner and more intuitive than Promise error catching)
  3. Debugging is also more readable and relatively friendly