Home > Article > Web Front-end > Detailed introduction to Promise in es6
Promise
Promise is an object from which messages for asynchronous operations can be obtained;
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);
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;
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)
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
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;
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;
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
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.
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 }) ); };
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!