First print Promis

In particular, note the resolve and reject parameters

Promise is a constructor. The ontology has all, Reject,resolve, and the prototype chain has then catch. So the object that comes out of a promise new must have the then catch method. A promise can be understood as a container that usually contains the result of an asynchronous operation. Syntactically, a promise is an object that can fetch information about an asynchronous operation, and all asynchronous operations can be handled in the same way

Promise objects have the following two characteristics:

(1) The state of the object is not affected by the outside world. The Promise object represents an asynchronous operation and has three states: Pending, Resolved, and Rejected. Only the result of an asynchronous operation can determine the current state, and no other operation can change the state. That’s where the name “Promise” comes from. Its English name means “Promise,” indicating that nothing else can change it.

(2) Once the state changes, it will never change again, and this result can be obtained at any time. There are only two possibilities for a Promise object to change state: from Pending to Resolved and from Pending to Rejected. As long as those two things happen, the state is frozen, it’s not going to change, it’s going to stay the same. If you add a callback to the Promise object, you’ll get the same result immediately, even if the change has already occurred. This is quite different from an Event, which has the characteristic that if you miss it and listen again, you will not get the result.

With the Promise object, asynchronous operations can be expressed as a flow of synchronous operations, avoiding layers of nested callback functions. In addition, Promise objects provide a unified interface that makes it easier to control asynchronous operations.

In a nutshell, Promise is written asynchronously in a synchronous fashion to solve the callback problem

Then () method

Then method is to separate the original callback writing method, after the completion of asynchronous operation, use the method of chain call to execute the callback function, the advantage of promise is called when filing, you can continue to write the promise object in then method and return, and continue to call THEN to execute the callback operation

The two most important parameters are reslove,reject

Reject ()

Reject = reject (reject); reject (reject) = reject (reject); reject (reject) = reject (reject); I’m going to do catch()

All () method

Promise’s all method provides the ability to execute asynchronous operations in parallel by agreeing to perform multiple asynchronous operations, and then() will execute then() only after they are done by combining the results of all asynchronous operations into an array and passing them to THEN.

The difference between the race() method and All

Promise’s race method, which literally means a race, differs from the all method in that it executes then callbacks as soon as another asynchronous operation completes and reaches the finish line first

Asynchronous instance