SOURCE

const isFunction = target => typeof target === 'function';

class MyPromise {
    _state = 'PENDING';
    _result = null;
    _fulfilledCallbacks = [];
    _rejectedCallbacks = [];
    constructor(handle) {
        if (!isFunction(handle)) {
            throw new Error('MyPromise must accept a function as a parameter')
        }

        try {
            handle(this._resolve, this._reject)
        } catch (err) {
            this._reject(err)
        }
    }

    _resolve = res => {
        if (this._state !== 'PENDING') return;
        this._state = 'FULFILLED';
        this._result = res;
        setTimeout(() => {
            this._fulfilledCallbacks.forEach(cb => {
                cb(res);
            })
        }, 0)
    }

    _reject = res => {
        if (this._state !== 'PENDING') return;
        this._state = 'REJECTED';
        this._result = res;
        setTimeout(() => {
            this._rejectedCallbacks.forEach(cb => {
                cb(res);
            })
        }, 0)
    }

    then = (onFulfilled, onRejected) => {
        const { _value, _state } = this;
        return new MyPromise((onFulfilledNext, onRejectedNext) => {
            let fulfill = value => {
        console.log(onFulfilled)
                try {
                    if (!isFunction(onFulfilled)) {
                        onFulfilledNext(value);
                    } else {
                        let res = onFulfilled(value);
                        if (res instanceof MyPromise) {
                            res.then(onFulfilledNext, onRejectedNext);
                        } else {
                            onFulfilledNext(value);
                        }
                    }
                } catch (err) {
                    onRejectedNext(err);
                }
            }

            let reject = value => {
                try {
                    if (!isFunction(onRejected)) {
                        onRejectedNext(value);
                    } else {
                        let res = onRejected(value);
                        if (res instanceof MyPromise) {
                            res.then(onFulfilledNext, onRejectedNext);
                        } else {
                            onFulfilledNext(value);
                        }
                    }
                } catch (err) {
                    onRejectedNext(err);
                }
            }

            switch (_state) {
                // 当状态为pending时,将then方法回调函数加入执行队列等待执行
                case 'PENDING':
                    this._fulfilledCallbacks.push(fulfill)
                    this._rejectedCallbacks.push(reject)
                    break
                // 当状态已经改变时,立即执行对应的回调函数
                case 'FULFILLED':
                    fulfill(_value);
                    break
                case 'REJECTED':
                    reject(_value);
                    break
            }
        });
    }
}

new MyPromise((resolve, reject) => {
    resolve(1);
}).then((res) => {
    console.log(123);
})
console 命令行工具 X clear

                    
>
console