Javascript Promise learning notes, javascriptpromise

Source: Internet
Author: User

Javascript Promise learning notes, javascriptpromise

1. Definition: Promise is a component that abstracts asynchronous processing objects and performs various operations on them. It uses asynchronous processing objects and asynchronous processing rulesUnified Interface.

 

2. API defined in ES6 Promises standard:

A)Constructor: new is used for instantiation,
Var promise = new Promise (function (resolve, reject ){...});

B)Instance Method: Use the then Method to set the callback function for successful (resolve) and failed (reject,

Promise. then (onFulfilled, onRejected ),

Promise. catch (onRejected), where catch is only the alias of then;

C)Static Method: some other auxiliary methods, such as all () and resolve ().

 

3. Promise status. Three statuses: Pending (initialization), Fulfilled, and Rejected. The function executed after then is certainly called only once.

Processing onFulfilled when the promise object is resolve

Processing onRejected when the promise object is reject

Figure 1. Execution Process of Promise call

 

4. Resolve Method:

A)The static method Promise. resolve (value) can be considered as a shortcut of the new Promise () method, which is a syntactic sugar; Promise. reject (error) is similar to it;

B)Another function of the Promise. resolve method is to convert a thenable object to a promise object, such as jQuery. ajax ().

 

5. Promise specificationsPromiseOnly the asynchronous call method can be used.. Therefore, promise. then is actually an asynchronous call.

 

6. Promise method chain: Promise can connect any method together as a method chain ). For example:

. Then (taskA). then (taskB). catch (onRejected). then (finalTask );

Method chains concatenate methods and strictly execute them in sequence.

* Note: catch in ECMAScript 3 is a reserved word, so it cannot be used in IE8. Therefore, it is generally replaced by promise ["catch"] Or then.

Figure 2. Execution Process of Promise chained call

 

7. Every time then is called, a newly created promise object will be returned. You should try to avoid a promise from executing multiple then methods, but use the chain call method.

 

8. Promise and array: Promise. all ([…]) It is called after all promise objects change to the FulFilled or Rejected state. All Promise methods in the promise object array are called at the same time.

Promise. race is called after any method changes to FulFilled or Rejected state. After the first promise object changes to Fulfilled, the execution of other promise objects is not canceled.

 

9. then and catch in Promise:

A)If an exception occurs in onFulfilled using promise. then (onFulfilled, onRejected), this exception cannot be caught in onRejected.

B)In the case of promise. then (onFulfilled). catch (onRejected), exceptions generated by then can be caught in. catch.

C)There is no difference between. then and. catch in nature and should be used in different scenarios.

 

10. Deferred owns Promise. Deferred and Promise are not in a competitive relationship, but Deferred implies Promise. To use Deferred, you only need to create a deferred object. You can call the resolve and reject methods at any time.
Var deferred = new Deferred ();

This article is excerpted from the JavaScript Promise Mini Book (Chinese Version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.