SOURCE

const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

function FunctionPromise(fn) {
    this.status = PENDING;
    this.value = null;
    this.reason = null;
    this.onFulfilledCallbacks = [];
    this.onRejectedCallbacks = [];

    const that = this;
    // 静态方法
    function resolve(value) {
        if (that.status === PENDING) {
            that.status = FULFILLED;
            that.value = value;
            that.onFulfilledCallbacks.forEach(callback => {
                callback(that.value)
            })
        }
    }
    function reject(reason) {
        if (that.status === PENDING) {
            that.status = REJECTED;
            that.reason = reason;
            that.onRejectedCallbacks.forEach(callback => {
                callback(that.reason)
            })
        }
    }

    try {
        // 为什么是这么调用?
        fn(resolve, reject)
    } catch (e) {
        reject(e)
    }
}

// 原型方法
FunctionPromise.prototype.then = function (onFulfilled, onRejected) {
    // 判断是否为回调函数
    if(typeof onFulfilled !== 'function') {
        onFulfilled = function (value) {
            return value
        }
    }
    if(typeof onRejected !== 'function') {
        onRejected = function (reason) {
            return reason
        }
    }


}
console 命令行工具 X clear

                    
>
console