手写promise(简易版)

class myPromise{
    constructor(fn){
        this.statusObj = {
            PENDING:"PENDING",
            RESOLVED:"RESOLVED",
            REJECTED:"REJECTED",
        }
        this.value = null;//传递给then方法的value值
        this.status = this.statusObj.PENDING;
        this.resolvedCallBacks = [];
        this.rejectedCallBacks = [];
        myPromise.that = this;
        try {
            fn(myPromise.resolve,myPromise.rejected);//执行bew myPromise
        } catch (error) {
            myPromise.rejected(this.value);
        }
    }
    /**
     * 静态resolve方法,MyPromise实例不可访问;
     * 支持类MyPromise访问,例:MyPromise.resolve('success').then(e=>e)
     * @param value 
     */
    static resolve(value){
        const that = myPromise.that;// 由于静态方法内的this访问的是类还不是实例,所以使用这种办法访问对象实例
        const f = that instanceof myPromise
        if(f&&that.status === that.statusObj.PENDING){
            that.status = that.statusObj.RESOLVED;
            that.value = value;
            that.resolvedCallBacks.map(cb => that.value = cb(that.value));
        }
        // MyPromise 类访问resolve
        if(!f){
            const obj = new myPromise()
            return Object.assign(obj,{
                status:obj.statusObj.RESOLVED,
                value
            })
        }
    }

    static rejected(value){
        const that = myPromise.that;
        const f = that instanceof myPromise;
        if(f&&that.status == that.statusObj.REJECTED){
            that.status = that.statusObj.REJECTED;
            that.value = value;
            that.rejectedCallBacks.map(cb => that.value = cb(that.value))
        }
        if(!f){
            const obj = new myPromise();
            return obj.assign(obj,{
                status:that.statusObj.REJECTED,
                value
            })
        }
    }
    /**
     * MyPromise 原型上的then方法
     * @param onFulfilled 
     * @param onRejected 
     */
    then(onFulfilled,onRejected){
        const {PENDING,RESOLVED,REJECTED} = this.statusObj
        const f = typeof onFulfilled === 'function' ? onFulfilled:c => c;
        const r = typeof onRejected === 'function' ? onRejected : c => {throw c}
        switch (this.status) {
            case PENDING:
                    this.resolvedCallBacks.push(f)
                    this.rejectedCallBacks.push(f);
            break;
            case RESOLVED:
                this.value = f(this.value)// 将回调函数的返回值赋值给实例的 value ,满足链式调用then方法时传递value
            break;
            case REJECTED:
                this.value = r(this.value)//  将回调函数的返回值赋值给实例的 value ,满足链式调用then方法时传递value
            break;
            default:
                break;
        }
        return this;
    }
}

// new myPromise(resolve => {
//     setTimeout(() => {
//         resolve(1)
//     }, 2000);
// }).then((res2) => {
//         console.log(res2);
//         return 2;
//     }).then((res3) => {
//         console.log(res3);
//     })

myPromise.resolve('success').then((e) => {
    console.log(e);
    return e + 1;
}).then((res) => {
    console.log(res);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值