手写Promise

class HD {
        static PENDING = 'pending'
        static FUIFUILED = 'fulfiled'
        static REJECTED = 'rejected'
        constructor(executor) {
            this.status = HD.PENDING;
            this.value = null;
            this.callbacks = [];
            executor(this.resolve.bind(this), this.reject.bind(this))
        }
        resolve(value) {
            if (this.status == HD.PENDING) {
                this.status = HD.FUIFUILED;
                this.value = value;
                setTimeout(() => {
                    this.callbacks.map(el => {
                        el.onSuccess()
                    })
                })
            }

        }
        reject(error) {
            if (this.status == HD.PENDING) {
                this.status = HD.REJECTED;
                this.value = error;
                setTimeout(() => {
                    this.callbacks.map(el => {
                        el.onError()
                    })
                })
            }
        }
        then(onSuccess, one rror) {
            if (typeof onSuccess != 'function') {
                onSuccess = () => this.value//值穿透
            }
            if (typeof one rror != 'function') {
                one rror = () => this.value//值穿透
            }
            let promise = new HD((resolve, reject) => {
                if (this.status == HD.PENDING) {
                    this.callbacks.push({
                        onSuccess: () => {
                            this.parse(promise, onSuccess(this.value), resolve, reject)
                        },
                        one rror: () => {
                            let result = one rror(this.value)
                            resolve(result)
                        }
                    });
                }
                if (this.status == HD.FUIFUILED) {
                    setTimeout(() => {
                        this.parse(promise, onSuccess(this.value), resolve, reject)
                    })
                }
                if (this.status == HD.REJECTED) {
                    setTimeout(() => {
                        this.parse(promise, one rror(this.value), resolve, reject)
                    })
                }
            });
            return promise;
        }
        parse(promise, result, resolve, reject) {
            //引入promise是因为promise不能返回它自己
            if (promise == result) {
                throw new Error('完全错误')
            }
            try {
                if (result instanceof HD) {
                    result.then(resolve, reject)
                } else {
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }

        }
        static resolve(value) {
            return new HD((onFulfiled, onReject) => {
                if (value instanceof HD) {
                    value.then(onFulfiled, onReject)
                } else {
                    onFulfiled(value)
                }
            })
        }
        static reject(value) {
            return new HD((onFulfiled, onReject) => {
                if (value instanceof HD) {
                    value.then(onFulfiled, onReject)
                } else {
                    onReject(value)
                }
            })
        }
        static all(promises) {
            let values = []
            return new HD(((resolve, reject) => {
                promises.map(el => {
                    el.then(res => {
                        values.push(res);
                        if (values.length == promises.length) {
                            resolve(values)
                        }
                    }, error => {
                        reject(error)
                    })
                })
            }))
        }
        static race(promises) {
            return new HD((resolve, reject) => {
                promises.map(el => {
                    el.then(res => {
                        resolve(res);
                    }, error => {
                        reject(error);
                    })
                })
            })
        }
    }

 

上一篇:Promise 实现原理


下一篇:promise的自定义代码实现