Take you to explore the promise

First, tell us about the characteristics of promise:

1, once the state Promise of change can not be changed.

The compiler enter the code:

In the console print as follows:

 

 

 

 

Then enter in the compiler:

In the console print as follows:

 

 You can see the status of the first and still the same success. Similarly, if the first execution of a reject then his status will not change.

 to sum up:

 promise has three states:
1, pending [Pending] original state;
2, fulfilled [implement] operation is successful, resolve this case is operated;
3, rejected [rejected] operation failed;
When the status changes promise, it will trigger the then () function in response to the subsequent steps;
a promise altered state, will not change.
Promise object changes state, only two possibilities:
Changes from pending fulfilled (resolve this case is operated)
Changes from pending rejected (reject is operated at this time)
As long as these two cases occurred on the solidification state, it will not be changed.

 

2, then the desired parameter is a function of the method Promise, passing a non-penetration value function will occur.

  .then we expect a transfer method, but you can not pass a method, when there are two .then, when you do not pass the first method, as it had happened, to penetrate the second .then . Similarly, if the second is not, then a method will always penetrate down.

 

3, Promise callback is synchronized, and then the callback is asynchronous.

  

 

 

 

   

 

 

   It can be seen that the code is shaded portion promise are synchronized, and then the inside code is printed last asynchronous.

 

4, then calling chain, then a return value is received then the next parameter, if an error is returned, it will return a reject status of promise.

  

 

   

 

   Then you can see the parameters of the second parameter is the first to get the return value. Then if it returns an error how to do it? Consider the following codes:

  

 

   

 

   

 

5, then the pullback in return a promise will enter a waiting state until the promise of changing the return.

  

 

   

 

   then waits until the implementation of the method which changes state after promise after two seconds.

 

 

 

 

new Promise(( resolve, reject) =>{
console. log( 1)
resolve( 'succsee')
}). then(( res) =>{
console. log( 2)
})
console. log( 3)

Guess you like

Origin www.cnblogs.com/yi-yezhiqiu/p/11562817.html