手动封装promise功能,class封装实现

文章介绍了如何使用JavaScript的class语法手动封装一个Promise对象,包括构造函数接收executor回调,设置成功和失败状态,处理回调队列以及实现then方法,支持链式调用。通过这个实现,读者可以更好地理解Promise的工作原理和内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手动封装promise功能,class封装实现

promise基本功能点

  1. 创建对象的时候,传入一个回调,并且能够接收成功、失败状态设置两个参数
  2. 能够通过then方法调用,根据状态调用成功或者失败回调,then后面还可以连续使用then,因此then方法需要返回与当前状态一致的promise

实现promise基本功能

class MyPromise = {
	// 构造函数
	constructor(executor) {
	// 定义状态和初始值
        this.state = 'pending'
        this.value = undefined
        // 回调队列
        this.handlers = []
        // 设置成功状态
        const resolve = (value) => {
            if (this.state === 'pending') {
                this.state = 'fulfilled';
                this.value = value;
                this.handlers.forEach(handler => this.executeHandler(handler));
                this.handlers = [];
            }
        }
        // 设置失败状态
        const reject = (reason) => {
            if (this.state === 'pending') {
                this.state = 'rejected';
                this.value = reason;
                this.handlers.forEach(handler => this.executeHandler(handler));
                this.handlers = [];
            }
        };
        try {
            // 将回调传出去给参数给回调使用来设置状态
            executor(resolve, reject);
        } catch (error) {
            reject(error);
        }
    }
	// 回调执行
	executeHandler(handler) {
        // 统一处理回调
        const { onFulfilled, onRejected, resolve, reject } = handler;

        if (this.state === 'fulfilled') {
            // 成功状态执行
            if (typeof onFulfilled === 'function') {
                try {
                    // 成功执行,成功状态
                    const result = onFulfilled(this.value);
                    resolve(result);
                } catch (error) {
                    // 失败执行
                    reject(error);
                }
            } else {
                resolve(this.value);
            }
        } else if (this.state === 'rejected') {
            if (typeof onRejected === 'function') {
                try {
                    // 成功执行,失败状态
                    const result = onRejected(this.value);
                    resolve(result);
                } catch (error) {
                    // 失败执行
                    reject(error);
                }
            } else {
                // 失败执行
                reject(this.value);
            }
        }
    }
    // then方法
    then(onFulfilled, onRejected) {
        // then需要返回promise对象
        return new MyPromise((resolve, reject) => {
            // resolve和reject成功用来设置这个新返回的promise状态和当前的一致
            const handler = {
                onFulfilled,
                onRejected,
                resolve,
                reject
            };

            if (this.state === 'pending') {
                // 如果是pending,添加回调进队列
                this.handlers.push(handler);
            } else {
                this.executeHandler(handler);
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值