SOURCE

class myPromise {
    constructor(fn) {
        this.state = 'pending';
        this.val = '';
        this.fulfilledCbList = [];
        this.rejectedCbList = [];
        fn(this.resolve.bind(this), this.reject.bind(this));
    }
    resolve(val) {
        if (this.state = 'pending') {
            this.state = 'fulfilled';
            this.val = val;
        }
    }
    reject() {
        if (this.state = 'pending') {
            this.state = 'rejected';
            this.val = val;
        }
    }

    then(successCb, failCb) {
        if (this.state == 'pending') {
            this.fulfilledCbList.push(successCb);
            this.rejectedCbList.push(failCb);
            return;
        }
        else if (this.state == 'rejected') {
            this.rejectedCbList.map(cb => cb());
        }
        else {
            this.fulfilledCbList.map(cb => cb());
        }
    }

}
console 命令行工具 X clear

                    
>
console