当前位置:网站首页>Handwritten promise [05] - exception capture of promise method and optional parameters of then method implementation

Handwritten promise [05] - exception capture of promise method and optional parameters of then method implementation

2022-06-11 06:25:00 Geek student

1. Capture customization LyPromise Mistakes in

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class LyPromise {
    
    constructor(executor) {
    
        try {
    
            executor(this.resolve, this.reject);
        } catch(error) {
    
            this.reject(error);
        }
    }

    status = PENDING;

    value = undefined;
    reason = undefined;

    //  Successful callback 
    successCallback = [];
    //  Failed callback 
    failCallback = [];

    resolve = (value) => {
    
        if (this.status !== PENDING) return;
        this.status = FULFILLED;
        this.value = value;
        //  Determine whether a successful callback exists , If there is   Call the successful callback 
        // this.successCallback && this.successCallback(this.value);
        while (this.successCallback.length) {
    
            this.successCallback.shift()(this.value); // perform 
        }

    }

    reject = (reason) => {
    
        if (this.status !== PENDING) return;
        this.status = REJECTED;
        this.reason = reason;
        //  Determine whether the failed callback exists , If there is   Call the failed callback 
        // this.failCallback && this.failCallback(this.reason)
        while (this.failCallback.length) {
    
            this.failCallback.shift()(this.reason)
        }

    }

    then(successCallback, failCallback) {
    
        let Promise2 = new LyPromise((resolve, reject) => {
    
            if (this.status === FULFILLED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = successCallback(this.value);
                    resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        // Q:  Why want to use  reject
                        // A:  call chaining  then  Method time , The last one that went wrong  then  Need to be in the next  then  Method 
                        reject(error);
                    }
                }, 0)
            } else if (this.status === REJECTED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = failCallback(this.reason);
                        resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        reject(error);
                    }
                }, 0)
            } else {
    
                //  When the actuator operates asynchronously ,status still pending,
                //  So at this time, we don't know whether it is a success or a failure callback , So here we need to call back these two 
                //  Store it 
                this.successCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = successCallback(this.value);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
                this.failCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = failCallback(this.reason);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
            }
        })

        return Promise2;

    }
}


function resolvePromise(promise2, x, resolve, reject) {
    
    if (promise2 === x) {
    
        return reject(new TypeError("Promise  Referenced circularly !!!"))
    }

    if (x instanceof LyPromise) {
     // promise object 
        x.then(resolve, reject)
    } else {
      //  Common value 
        resolve(x);
    }

}

module.exports = LyPromise;

2. then Method becomes an optional parameter

stay Promise in then Methods Successful callback and Failed callback Is an optional parameter , It belongs to what can be written but not written .

let promise = new Promise((resolve, reiect) => {
    
    resolve(100);
});

promise
    .then()
    .then()
    .then()
    .then((value) => {
    
    console.log(value);  // 100
});

When in the previous then When no parameters are passed in the method , In fact, it is equivalent to passing a Returned to himself Callback function for :
 Insert picture description here

So just judge this then It should be ok if the method passes parameters , If no parameters are passed , Then give him a parameter .

  then(successCallback, failCallback) {
    
        successCallback = successCallback ? successCallback : value => value;
        failCallback = failCallback ? failCallback : reason => reason;
        let Promise2 = new LyPromise((resolve, reject) => {
    
            if (this.status === FULFILLED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = successCallback(this.value);
                        resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        // Q:  Why want to use  reject
                        // A:  call chaining  then  Method time , The last one that went wrong  then  Need to be in the next  then  Method 
                        reject(error);
                    }
                }, 0)
            } else if (this.status === REJECTED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = failCallback(this.reason);
                        resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        reject(error);
                    }
                }, 0)
            } else {
    
                //  When the actuator operates asynchronously ,status still pending,
                //  So at this time, we don't know whether it is a success or a failure callback , So here we need to call back these two 
                //  Store it 
                this.successCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = successCallback(this.value);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
                this.failCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = failCallback(this.reason);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
            }
        })

        return Promise2;

    }

Test it :

 Insert picture description here

 Insert picture description here

3. All current codes

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class LyPromise {
    
    constructor(executor) {
    
        try {
    
            executor(this.resolve, this.reject);
        } catch(error) {
    
            this.reject(error);
        }
    }

    status = PENDING;

    value = undefined;
    reason = undefined;

    //  Successful callback 
    successCallback = [];
    //  Failed callback 
    failCallback = [];

    resolve = (value) => {
    
        if (this.status !== PENDING) return;
        this.status = FULFILLED;
        this.value = value;
        //  Determine whether a successful callback exists , If there is   Call the successful callback 
        // this.successCallback && this.successCallback(this.value);
        while (this.successCallback.length) {
    
            this.successCallback.shift()(this.value); // perform 
        }

    }

    reject = (reason) => {
    
        if (this.status !== PENDING) return;
        this.status = REJECTED;
        this.reason = reason;
        //  Determine whether the failed callback exists , If there is   Call the failed callback 
        // this.failCallback && this.failCallback(this.reason)
        while (this.failCallback.length) {
    
            this.failCallback.shift()(this.reason)
        }

    }

    then(successCallback, failCallback) {
    
        let Promise2 = new LyPromise((resolve, reject) => {
    
            if (this.status === FULFILLED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = successCallback(this.value);
                        resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        // Q:  Why want to use  reject
                        // A:  call chaining  then  Method time , The last one that went wrong  then  Need to be in the next  then  Method 
                        reject(error);
                    }
                }, 0)
            } else if (this.status === REJECTED) {
    
                setTimeout(() => {
    
                    try {
    
                        let x = failCallback(this.reason);
                        resolvePromise(Promise2, x, resolve, reject);
                    } catch (error) {
    
                        reject(error);
                    }
                }, 0)
            } else {
    
                //  When the actuator operates asynchronously ,status still pending,
                //  So at this time, we don't know whether it is a success or a failure callback , So here we need to call back these two 
                //  Store it 
                this.successCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = successCallback(this.value);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
                this.failCallback.push(() => {
    
                    setTimeout(() => {
    
                        try {
    
                            let x = failCallback(this.reason);
                            resolvePromise(Promise2, x, resolve, reject);
                        } catch (error) {
    
                            reject(error);
                        }
                    }, 0);
                });
            }
        })

        return Promise2;

    }
}


function resolvePromise(promise2, x, resolve, reject) {
    
    if (promise2 === x) {
    
        return reject(new TypeError("Promise  Referenced circularly !!!"))
    }

    if (x instanceof LyPromise) {
     // promise object 
        x.then(resolve, reject)
    } else {
      //  Common value 
        resolve(x);
    }

}

module.exports = LyPromise;
原网站

版权声明
本文为[Geek student]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020527520863.html