When I was a child, crying is a magic weapon to solve problems. When I grow up, laughing is a weapon to face life.
Usage:
let promise1 = new Promise((resolve, reject) => {
resolve(1)
});
promise1.then(res => console.log(res))
Copy the code
New Promise(XXX) returns an object promise1, promise1. Then (()=>{}, ()=>{}). Resolve is a function parameter that is added to the properties of the then method, and the ability to change the promise instance returned by the current THEN method is stored in the methods of the previous instance.
/* * @Author: zhangbuzhou * @Date: 2021-07-24 21:42:47 * @LastEditTime: 2021-07-24 22:01:47 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \myvue-project-runtime-only\src\utils\MyPromise.js */ function MyPromise(excutorCallBack) { this.value = ""; this.status = "pending"; // Fulfiled, rejected this. This. RejectedCallbackArr = []; try { excutorCallBack(this.resolve, this.reject); } catch (err) { this.reject(err); }} // Async end: Then success, change the instance state, call the callback MyPromise. Prototype. Resolve = function (response) {/ / use is to reflect the asynchronous timer, SetTimeout (() => {// Once the state of the promise instance changes, it cannot be changed again. If (this.status === "pending") {this.value = response; if (this.status == pending) {this.value = response; this.status = "fulfiled"; / / if the instance behind calls the method then, at this point has been added to the fulfiledCallbackArr array of enclosing fulfiledCallbackArr. ForEach (callback = > {callback (response); }); }}); }; MyPromise. Prototype. Reject = function (error) {/ / with resolve setTimeout (() = > {the if (this. The status = = = "pending") { this.value = error; this.status = "rejected"; this.rejectedCallbackArr.forEach(callback => { callback(error); }); }}); }; // When promise instance A calls then, A callback is added to promise instance A. // When promise instance A calls then, A call to resolve is made. If the state is changed, the then callback will be called. If the state is changed, the then callback will be called. If the state is changed, the then callback will be called. Mypromise.prototype. Then = function(resolveFn, rejectFn) {return new MyPromise((resolveFn, rejectFn)); Reject) => {reject) reject (reject) => {reject (reject) then (reject) === 'function'? null : resolveFn = res => res; typeof rejectFn === 'function' ? null : rejectFn = err => err; this.fulfiledCallbackArr.push(() => { try { let result = resolveFn(this.value); // If the then function returns a promise instance, then the state of that promise instance depends on the state of result. Result. Then (resolve) result instanceof MyPromise? result.then(resolve) : resolve(result); } catch (err) { reject(err); }}); this.rejectedCallbackArr.push(() => { try { let result = rejectFn(this.value); result instanceof MyPromise ? result.then(resolve) : resolve(result); } catch (err) { reject(err); }}); }); }; Mypromise. all = function (promiseArr) {let len = promisearr.length; let result = []; Return new MyPromise((resolve, resolve, reject) => { promiseArr.forEach(promiseObj => { promiseObj.then(res=> { len --; result.push(res); if(! len) { resolve(result); }})})})}Copy the code