Understanding Promise (3)

In then promise of success, we have not only failed state status, there may be a wait state, so we have to wait for the state to be processed

function  Promise(executor) {
    let  self  = this;
    self.value = null;
    self.reason = null;
    self.resolveCallback = [];
    self.rejectCallback = [];
    //设置默认状态
    self.status = 'pending'
    //成功函数
    function  resolve(value) {
        if(self.status=='pending') {
            self.status = 'success';
            //the console.log (self.value); 
            self.value =   ' successful operation ' ; 
            self.resolveCallback.forEach (Fn => Fn ()); // publisher 
        } 
    }; 
    // function fails 
    function Reject (reason) {
         IF (self.status == ' Pending ' ) { 
            self.status = ' fail ' ; 
            self.reason = ' fails to run ' ; 
            self.rejectCallback.forEach (Fn => Fn ()); // publisher 
        } 
    }
    // The default function is executed immediately passing two parameters 
    Executor (Resolve, Reject); 
} 
Promise.prototype.then =   function (OnResolve, OnReject) {
     // the console.log (this.value) 
        IF ( the this .status == ' Success ' ) { 
            OnResolve ( the this .Value); 
        } 
        IF ( the this .status == ' Fail ' ) { 
            OnReject ( the this .reason); 
        } 
        // third states in a wait state 
        IF ( the this .status == ' Pending' ) {
             // successful callback resolveCallback this array to push 
            the this .resolveCallback.push (() => { 
                OnResolve ( the this .Value); 
            }) 
            // the callback fails to push this array rejectCallback 
            the this .rejectCallback .push (() => { 
                OnReject ( the this .reason); 
            }) 
        } 
}; 
module.exports = Promise;
 // a function executor begins execution we come in two arguments method then calls again, then there are methods methods OnResolve , OnReject method
 // the then approach, we have a state of the start of the call is pending and, depending on the status of different functions,
 //In two different functions we determined as a start state are all pending => success or = Pending> Fail
 // modified state, and a display value is determined
 

We first state in the judgment process then, if this is considered pending state, we will deal with it

First state to declare success callback array is empty failure status callback array is empty

  self.resolveCallback = [];
    self.rejectCallback = [];

Callback array in the wait state we will be successful callback function array to push the success of states, the push will fail to function in a failed state

 // successful callback resolveCallback this array to push 
            the this .resolveCallback.push (() => { 
                OnResolve ( the this .Value); 
            }) 
            // the callback fails to push this array rejectCallback 
            the this .rejectCallback.push ( () => { 
                OnReject ( the this .reason); 
            })

We will pass into state value

 

Finally, the array function to traverse in their execution of function calls itself

  self.resolveCallback.forEach (the Fn => the Fn ()); // release

 

Guess you like

Origin www.cnblogs.com/guangzhou11/p/11299673.html