JavaScript Promise理解

definition

  First, be clear, Promise is asynchronous. JS EventLoop asynchronous operation is achieved by JS event loop mechanism.  

  MDN Web Doc interpretation of the Promise: Promise object is a proxy object (a proxy value), the value at the time of Promise proxy object creation may be unknown. It allows you to asynchronous operation for the success and failure of each respective binding processing method (handlers). This allows asynchronous method that can return value as the synchronization method, but not immediately return the final results of the implementation, but a representative of future results appear in the Promise object.

  promise object has three states:

  1, pending (pending)

  2, fulfilled (Resolved / to achieve)

  3, rejected (rejected / not implemented)

  can be converted into a unidirectional pending fulfilled, the check may be converted into rejected, but can not convert between Fulfilled and rejected.

  How to understand it? Here illustrated by a story.

story

  Wang sixth grade this year, under pressure at the beginning of a small rise, so the job is sometimes a little more, but Wang seems to go out and play.

  Dad Today brings good news, he gave Wang promise (Promise) said that if it does not rain on weekends, took Wang to the zoo.

  So, Wang every day praying weekend without rain (penging).

  Possible one: it does not rain on weekends, you can go to the zoo Wang friends (fulfilled).

  Possible II: rain, Wang can only be at home doing homework (rejected).

Examples

1  // represented by a Boolean values rain 
2 the let = ifRain to true ;
 . 3  
. 4 the let goToZoo = new new Promise ( function (Resolve, Reject) {
 . 5      // if it does not rain, go to the zoo 
. 6      IF ! ( IfRain) {
 . 7          Message = the let "Go to Zoo" ;
 . 8          Resolve (Message);
 . 9      }
 10      // rains homework 
. 11      the else {
 12 is          the let Message = new new Error ( "do homework" );
 13 is          Reject (Message);
 14     }
15 });
16 
17 let test = function () {
18     goToZoo.then(function (fulfilled) {
19         console.log(fulfilled);
20     }).catch(function (rejected) {
21         console.log(rejected.message);
22     })
23 };
24 
25 test();

  When ifRain is true, the output do homework; is false, outputs go to zoo.

  Promise usage here is:

  new Promise( function(resolve, reject) {...} /* executor */  );

  Here's resolve, reject parameter is a function, when the commitment to achieve a time, will resolve function calls, then the state becomes the corresponding Promise fulfilled; when the commitment is not achieved, it will call reject function, its status changes the rejected.

  executor is also a function. and reject resolve executor is a parameter transferred into, of course, these two parameters are optional filled. resolve or reject the call if it will result in the function as a parameter to pass out; resolve Description Promise achieved, it will result in a function to pass out, but reject it shows Promise is not achieved, it is the equivalent of a mistake , so the error message it will pass out as a parameter.

 

Guess you like

Origin www.cnblogs.com/tianwen1993/p/10950789.html