Home > Web Front-end > JS Tutorial > body text

Detailed introduction to Promise in es6

零下一度
Release: 2017-06-26 15:23:45
Original
1826 people have browsed it

Promise

  1. Promise is an object from which messages for asynchronous operations can be obtained;

  2. Features: The state of the object is not affected by the outside world Impact (Pending in progress, Resolved completed, Rejected failed), only the result of the asynchronous operation can determine the current state; once the state changes, it will not change (only from Pending to Resolved and Pending to Rejected);

  3. Disadvantages: Once created, it will be executed immediately and cannot be canceled midway; if there is no callback function, errors thrown internally cannot be reflected externally; when it is Pending, it is impossible to know which stage the current progress is;

  4. Generally do not define the callback function of the Reject state (that is, the second parameter of then) in the then method, but use the catch method; because this can capture the errors in the previous then, and also Closer to synchronous writing (try/catch)

  5. catch method still returns a Promise object, so you can also call the then method later; in the catch method, It can also throw an error

  6. The Promise.all method is used to package multiple Promise instances into a new Promise instance; the parameters of the Promise.all method can not be arrays, but must It has an Iterator interface, and each returned member is a Promise instance; only when the states of p1, p2, and p3 become fulfilled, the p state will become fulfilled; as long as there is one rejected, p will become rejected;

  7. Promise.race also wraps multiple Promise instances into new Promise; as long as the state of one object changes, the state of p will change accordingly, and the value of the first changed object will be returned and passed to p's callback function;

  8. Promise.resolve converts the object into a Promise object, and the status is resolved

    // 将thenable对象转为Promise对象var thenable = {
        then(resolve, reject) {
            resolve(200)
        }
    }var p = Promise.resolve(thenable)
    
    p.then((data) => {
      console.log(data)
    })  // 200
    Copy after login
  9. Promise.reject Returns a Promise object, and the instance status is rejected; the parameters of this method will remain unchanged as the reason for rejection and become the parameters of subsequent methods.

  10. Two additional methods

    // donePromise.prototype.done = function(onFulfilled, onRejected) {this.then(onFulfilled, onRejected)
            .catch(function(reason) {
                setTimeout(() => {throw reason}, 0)   
            });
    };// finallyPromise.prototype.finally = function (callback) {
        let P = this.constructor;return this.then(
            value  => P.resolve(callback()).then(() => value),
            reason => P.resolve(callback()).then(() => { throw reason })
        );
    };
    Copy after login

    done is used to capture errors that may occur at any time and throw them globally;
    finally is used for operations that will be executed regardless of the state of the Promise object. It accepts a common callback function as a parameter (must be executed);

The above is the detailed content of Detailed introduction to Promise in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template