Promise实现

"use strict";
// let p = new Promise((resolve, reject)=>{
//     resolve('成功了')
// })
// p.then((result) => {
//     console.log(result)
// })
// console.log(p,Promise)
class Promise1 {
    constructor(executor) {
        this['[[PromiseStatus]]'] = "pending";
        this['[[PromiseValue]]'] = undefined;
        this.taskQueue = {
            resolveQueue: [],
            rejectQueue: [],
        };
        this.msgList = [];
        if (typeof executor !== 'function') {
            throw new Error("未传入函数");
        }
        else {
            executor(this.resolve.bind(this), this.reject.bind(this));
        }
    }
    resolve(info) {
        if (this["[[PromiseStatus]]"] !== 'pending')
            return;
        this['[[PromiseStatus]]'] = 'resolved';
        this["[[PromiseValue]]"] = info;
        this.taskQueue.resolveQueue.forEach(item => {
            item(info);
        });
        this.public(true);
    }
    reject(info) {
        if (this["[[PromiseStatus]]"] !== 'pending')
            return;
        this['[[PromiseStatus]]'] = 'rejected';
        this["[[PromiseValue]]"] = info;
        this.taskQueue.rejectQueue.forEach(item => {
            item(info);
        });
        this.public(false);
    }
    then(resolveFn, rejectFn) {
        if (this["[[PromiseStatus]]"] !== 'pending')
            return this;
        this.taskQueue.resolveQueue.push(resolveFn);
        rejectFn && this.taskQueue.rejectQueue.push(rejectFn);
        return this;
    }
    catch(rejectFn) {
        if (this["[[PromiseStatus]]"] !== 'pending')
            return;
        rejectFn && this.taskQueue.rejectQueue.push(rejectFn);
    }
    subscribe(target, callback) {
        target.msgList.push(callback);
    }
    public(flag) {
        this.msgList.forEach(callback => {
            callback(flag);
        });
    }
    static all(promiseArr) {
        let p = new Promise1(() => { });
        let index = 0;
        for (let i = 0; i < promiseArr.length; i++) {
            p.subscribe(promiseArr[i], (flag) => {
                if (!flag) {
                    p.reject('失败了');
                    return;
                }
                if (flag) {
                    index++;
                    if (index === promiseArr.length)
                        p.resolve('全部成功了');
                }
            });
        }
        return p;
    }
}
let p1 = new Promise1((resolve, reject) => {
    console.log('开始请求接口');
    setTimeout(() => {
        reject('p2接口请求失败');
    }, 3000);
});
let p2 = new Promise1((resolve, reject) => {
    console.log('开始请求接口');
    setTimeout(() => {
        reject('p2接口请求失败');
    }, 5000);
});
// p1.then( (result)=> {
//     console.log(result)
// }).catch ( (result)=> {
//     console.log(result)
// })
Promise1.all([p1, p2]).then((result) => {
    console.log(result);
}).catch(result => {
    console.log(result);
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值