/* a Promise is a class in which an executor is passed, and the executor is immediately executed. This is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity, which is a pity. Fulfilled reject: The then method is defined in the prototype object, and the value of the rejected 4 / THEN callback is the value of the rejected 4 / then callback. The then method can be called more than once on the same PROMISE object. The then method can be called chained. Then behind the method to the callback function that is a callback function then method on the return value of 8, all, finally method write * / const PENDING = 'PENDING' const FULFILLED = 'fulfiled' const Constructor (this. Resolve,this.reject) {constructor(this. Reject){constructor(this. } Catch (e){this.reject(e)}} //Promise status status = PENDING // Success value = undefined // failure reason = undefined // success callback successCallback = []; // failCallback = []; Resolve = value => {// If the status is not wait, prevent the program from executing down if(this.status! This. Status = this. Value = value // Determine whether the successful callback exists this.successCallback && this.successCallback(this.value); While (enclosing successCallback. Length) enclosing successCallback. The shift () (). This value} reject = "reason = > {/ / if the status is not waiting, Prevents the program from executing down if(this.status! == PENDING) return // Change the status to failed this.status = REJECTED // Save the cause of the failed this.reason = reason // Check whether the failed callback exists this.failCallback&& this.failCallback(this.reason); While (enclosing failCallback. Length) enclosing failCallback. The shift () (enclosing a tiny)} then (successCallback, failCallback) {/ / parameter is optional successCallback = successCallback ? successCallback : value => value failCallback = failCallback ? failCallback: This is a big pity. This is a big pity. This is a big pity. SetTimeout (()=>{try{let x = successCallback(this.value) // Determine if x is a normal value or a promise object // If x is a normal value, call resolve / / if it's a promise view promsie object returned results / / according to the result of promise object returned again Decided to call the resolve or call reject resolvePromise (promise1, x, resolve, reject) }catch(e){reject(e)}},0)}else if(this.status === REJECTED){setTimeout(()=>{try{let x = FailCallback (this.reason) // Determine whether x is a normal value or a promise object // If it is a normal value, call resolve directly // If it is a promise object and check the promsie object's return // According to the result of promise object returned again Decided to call the resolve or call reject resolvePromise (promise1, x, resolve, reject)} the catch (e) {reject (e)}}, 0)} else {/ / Waiting to see success callback callbacks and failure / / stored enclosing successCallback. Push (= > {setTimeout () () (= > {try {let x = successCallback x (value) this. / / // If it is a normal value, call resolve directly. // If it is a promise object, look at the promsie object and decide whether to call resolve or reject based on the promise object ResolvePromise (promise1, x, resolve, reject)} the catch (e) {reject (e)}}, 0)}) enclosing failCallback. Push (() = > {/ / synchronization code into asynchronous code SetTimeout (()=>{try{let x = failCallback(this.reason) // Determine whether x is a normal value or a promise object. // If x is a normal value, call resolve. // If it is a promise object View the results of a promsie object returned / / according to the result of promise object returned again Decided to call the resolve or call reject resolvePromise (promise1, x, resolve, reject)} the catch (e) { Reject (e)}},0)}) {return promise1} /* A finally method is executed regardless of whether the current Promise object succeeds or fails. */ Finally (callback){return this.then(value=>{// Successful callback return Mypromise.resolve (callback()).then(()=>value)},reason=>{// failed callback myPromise.resolve (callback()).then(()=>{throw Reason})})} // Catch (failCallback) {return this. Then (undefined, failCallback) } static all(array){ let result = [] let index = 0 function addData (key, value) { result[key] = value; index++; if (index === array.length) { resolve(result); } } return new MyPromise((resolve,reject)=>{ for(let i = 0; i < array.length; I ++){let current = array[I] if(current instanceof MyPromise){// Then (value => addData(I, value), Reason => reject(reason))}else{// Common value addData(I, array[I]); } } }) } static resolve(value){ if(value instanceof MyPromise) return value return new MyPromise(resolve => Function resolvePromise (promise1, x, resolve, reject) { if(promise1 === x){ reject(new TypeError('Chaining cycle detected for promise #<Promise>')) } if (x instanceof MyPromise) {// Promise object x.tohen (resolve, reject); } else {resolve(x); } } module.exports = MyPromiseCopy the code