In the then method, improve the code whose state is “pending”

function Promise(executor){
    // Resolve and reject are functions
    
    // Add attributes
    this.PromiseState = "pending";
    this.PromiseResult = null
    
    this.callacks = []
    
    const self = this;
    / / resolve function
    function resolve(data){
        if(self.PromiseState ! = ='pending') return
        // (1) Change the state of the object
        // this.promisestate // This is window because resolve is called directly
        self.PromiseState = 'fulfilled'
        // (2) Set the object result value
        self.PromiseResult = data
        self.callbacks.forEach(item= > {
            item.onResolved(self.PromiseResult)
        })
        
    }
    
    / / reject function
    function reject(data){
       if(self.PromiseState ! = ='pending') return
       // (1) Change the state of the object
        self.PromiseState = 'rejected'
        // (2) Set the object result value
        self.PromiseResult = data
        if(self.callbacks.onRejected){
            self.callbacks.onRejected(self.PromiseResult)
        }
        self.callbacks.forEach(item= > {
            item.onRejected(self.PromiseResult)
        })
    }
    
    // Call the executor function synchronously
    try {
        executor(resolve,reject)
    }catch(e){
        reject(e)
    }
    
}

// Add the then method
Promise.prototype.then = function(onResolved,onRejected){
const self = this
    return new Promise((resolve,reject) = > {
        if(this.PromiseState == 'fulfilled') {try{
                //onResolved(this.PromiseResult)
                let result = onResolved(this.PromiseResult)
                // Determine if result is a Promise object
                if(result instanceod Promise) {// If it is an object of type Promise
                    result.then(v= > {
                        resolve(v)
                    })
                }else{
                    // The object status of the result is success
                    resolve(result)
                }
           }catch(e){
               reject(e)
           }
        }
        if(this.PromiseState == 'rejected'){
            onRejected(this.PromiseResult)
        }

        // Determine pending status
        if(this.PromiseState == 'pending') {// Save the callback function so that it can be called when the state changes
           this.callacks.push({
               onResolved:function(){
                   try{
                       // Specify a successful callback function
                       let result = onResolved(self.PromiseResult)
                       if(result instanceod Promise) {// If it is an object of type Promise
                            result.then(v= > {
                                resolve(v)
                            },r= > {
                                reject(r)
                            })
                        }else{
                            // The object status of the result is success
                            resolve(result)
                        }
                     }catch(e){
                           reject(e)
                     }
               },
               onRejected:function(){
                   try{
                       let result = onRejected(self.PromiseResult)
                       if(result instanceod Promise) {// If it is an object of type Promise
                            result.then(v= > {
                                resolve(v)
                            },r= > {
                                reject(r)
                            })
                        }else{
                            // The object status of the result is success
                            resolve(result)
                        }
                   }catch(e){
                       reject(e)
                   }
               },
           })
        }
    })
}

Copy the code