What problems can be solved by using Promise? The use of async?

What problems can be solved by using Promise? The use of async?

Introduction: Promise is mainly used for asynchronous calculations, which can queue asynchronous operations and follow the directly expected results.

Three states:

pending (waiting state)
fulfiled (success state)
rejected (failure state)
once the state changes, it will not change, either success or failure.

Two parameters:

They are resolve and reject. When the state changes successfully, it will return a success result through resolve, and if it fails, it will return a failure result through reject.

To avoid freezing of the interface (task)

Synchronization:
Suppose you go to a restaurant, find a location, and call the waiter. At this time, the waiter tells you, I'm sorry I am a "synchronization" waiter, and I have to finish serving this table before I can greet you. The guests at that table obviously have already eaten, you just want a menu, such a small action, but the waiter wants you to wait until someone else’s big action is completed before they come to greet you again. This is a problem of synchronization: that is, "order". The work delivered 1234 must be completed in the order of 1234".

asynchronous:

After handing over the time-consuming work of A delivery to the system, it will continue to do the work of B delivery. After the system has completed the previous work, it can continue to do the remaining work of A through callbacks or events.
The order of completion of AB's work has nothing to do with the order in which they are delivered, so it is called "asynchronous".

In a word, the async function is syntactic sugar for the Generator function.

A generator is a new data type introduced by the ES6 standard. A generator looks like a function, but it can return multiple times.

Guess you like

Origin blog.csdn.net/weixin_54163765/article/details/112723850