手写Promise

该文章描述了一个自定义的Promise类实现,包括构造函数接收executor参数来执行resolve和reject,管理pending、fulfilled和rejected三种状态,以及then方法处理成功和失败的回调函数,支持链式调用。同时,文章涉及到对thenable对象的处理和错误捕获机制。
摘要由CSDN通过智能技术生成
class myPromise {
        constructor (executor) {
            this.status = 'pending';
            this.result = null;
            this.reason = null;
            this.onFullfilledCallBacks = [];
            this.onRejectedCallBacks = [];
            try {
                constructor(this.resolve.bind(this), this.reject.bind(this));
            } catch (error) {
                this.reject(error);
            }
        }
        resolve (result) {
            if (this.status = 'pending') {
                this.status = 'fullfilled';
                this.result = result;
                this.onFullfilledCallBacks.forEach(callback => {
                    callback(result);
                });
            }
        }
        reject (reason) {
            if (this.status = 'pending') {
                this.status = 'rejected';
                this.reason = reason;
                this.onRejectedCallBacks.forEach(callback => {
                    callback(reason);
                });
            }
        }
        then (onFullfilled, onRejected) {
            let promise2 = new Promise((resolve, reject) => {

                if (this.status = 'fullfilled') {
                    setTimeout(() => {
                        try {
                            if (typeof onFullfilled !== 'function') {
                                resolve(reason);
                            } else {
                                let x = onFullfilled(this.result);
                                resolvePromise(promise2, x, resolve, reject);
                            }
                            
                        } catch (error) {
                            reject(error);
                        }
                    });
                    
                } else if (this.status = 'rejected') {
                    setTimeout(() => {
                        try {
                            if (typeof onRejected !== 'function') {
                                resolve(reason);
                            } else {
                                let x = onRejected(this.reason);
                                resolvePromise(promise2, x, resolve, reject);
                            }
                        } catch (error) {
                            reject(error);
                        }
                    });
                } else if (this.status = 'pending') {
                    this.onFullfilledCallBacks.push(() => {
                        setTimeout(() => {
                            try {
                                if (typeof onFullfilled !== 'function') {
                                    resolve(reason);
                                } else {
                                    let x = onFullfilled(this.result);
                                    resolvePromise(promise2, x, resolve, reject);
                                }
                            } catch (error) {
                                reject(error);
                            }
                            
                        });
                    });
                    this.onRejectedCallBacks.push(() => {
                        setTimeout(() => {
                            try {
                                if (typeof onRejected !== 'function') {
                                    resolve(reason);
                                } else {
                                    let x = onRejected(this.reason);
                                    resolvePromise(promise2, x, resolve, reject);
                                }
                            } catch (error) {
                                reject(error);
                            }
                            
                        });
                    });
                }
            });
            return promise2;
        }
    }
    function resolvePromise (promise2, x, resolve, reject) {
        if (x === promise2) {
            throw new TypeError('Promise error');
        }
        if (x instanceof myPromise) {
            x.then(y => {
                resolvePromise(promise2, y, resolve, reject);
            }, reject)
        } else if (x !== null && ((typeof x === 'object' || (typeof x === 'function')))) {
            let called = true;
            try {
                let then = x.then;
            } catch (error) {
                return reject(error);
            }
            if (typeof then === 'function') {
                try {
                    then.call(
                        x,
                        y => {
                            if (called) return;
                            called = true;
                            resolvePromise(promise2, y, resolve, reject);
                        },
                        r => {
                            if (called) return;
                            called = true;
                            reject(r);
                        }
                    )
                } catch (error) {
                    if (called) return;
                    called = true;
                    reject(error);
                }
            } else {
                resolve(x);
            }
        } else {
            return resolve(x);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值