ES6 Proise simple to understand

Promise This is the object of a handle asynchronous ES6 in increased.

Traditional became time to write asynchronous functions, often encounter sets the callback callback:

 

Promise is asynchronous programming a solution ----- more reasonable and powerful than traditional solutions ----- callback functions and events.

The so-called Promise, it simply is a container, which holds the event will end sometime in the future (usually an asynchronous operation) results.

Syntactically, Promise is an object, you can get messages from the asynchronous operation of it. Promise to provide a unified API, a variety of asynchronous operations can be handled the same way.

PromiseObject has the following two characteristics.

State (1) is not subject to outside influence. PromiseObject represents an asynchronous operation, there are three states: pending(ongoing), fulfilled(been successful) and rejected(failed).

  Only results of asynchronous operations, you can decide what kind of current state, no other operations can change that state. This is also Promisethe origin of the name, which in English means "commitment", he said other means can not be changed.

(2) Once the status change, it will not change, at any time this result can be obtained. PromiseState of the object changes,

  There are only two possibilities: from pendingchanges to fulfilledand from pendingturns rejected. As long as these two things happens on the solidification state, it will not be changed and will keep the result, then called resolved (finalized).

  If the change has occurred, you again Promiseadd an object callback function will immediately get this result. This event (Event) is completely different, the characteristics of the event is, if you missed it, go listen, is not the result.

 The basic syntax:

DEMO, After reading test.txt read text2.txt

 

Guess you like

Origin www.cnblogs.com/lixiao0703/p/lixiao05.html