手写Promise

159 阅读1分钟

手写Promise

class MyPromise {
            constructor(executor) {
                this.status = 'pending'//初始状态为等待
                this.value = null//成功的值
                this.reason = null//失败的原因
                this.onFulfilledCallbacks = []//成功的回调函数数组
                this.onRejectedCallbacks = []//失败的回调函数数组
                let resolve = value => {
                    if (this.status === 'pending') {
                        this.status = "frlfilled"
                        this.value = value
                        this.onFulfilledCallbacks.forEach(fn => fn())//调用成功的回调函数
                    }
                }
                let reject = reason => {
                    if (this.status === 'pending') {
                        this.status = 'rejected'
                        this.reason = reason
                        this.onFulfilledCallbacks.forEach(fn => fn())//调用失败的回调函数
                    }
                }
                try {
                    executor(resolve, reject)
                } catch (err) {
                    reject(err)
                }
            }
            then(onFulfilled, onRejected) {
                return new MyPromise((resolve, reject) => {
                    if (this.status === 'fulfilled') {
                        setTimeout(() => {
                            const x = onFulfilled(this.value)
                            x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                        });
                    }
                    if (this.status === 'rejected') {
                        setTimeout(() => {
                            const x = onRejected(this.reason)
                               x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                        })
                    }
                    if (this.status === 'pending') {
                        this.onFulfilledCallbacks.push(() => {
                            setTimeout(() => {
                                const x = onFulfilled(this.value)
                                   x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                            });
                        })
                        this.onRejectedCallbacks.push(() => {
                            setTimeout(() => {
                                const x = onRejected(this.reason)
                                   x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                            })
                        })
                    }
                })
            }
        }
        //测试
        function p1() {
            return new MyPromise((resolve, reject) => {
                setTimeout(resolve, 1000, 1);
            })
        }
        function p2() {
            return new MyPromise((resolve, reject) => {
                setTimeout(resolve, 1000, 2);
            })
        }
        p1().then(res => {
            console.log(res);
            return p2()
        }).then(ret => {
            console.log(ret);
        })