SOURCE

const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';

class P {
    constructor(executor) {

        this.status = PENDING

        this.value = undefined

        this.reason = undefined

        // 存放成功的回调
        this.onResolvedCallbacks = [];
        // 存放失败的回调
        this.onRejectedCallbacks= [];

        let resolve = (value) => {
            if (this.status === PENDING) {
                this.status = FULFILLED
                this.value = value

                // 依次将对应的函数执行
                this.onResolvedCallbacks.forEach(fn=>fn());
            }
        }

        let reject = (reason) => {
            if (this.status === PENDING) {
                this.status = REJECTED
                this.reason = reason

                // 依次将对应的函数执行
                this.onRejectedCallbacks.forEach(fn=>fn());
            }
        }

        try{
            executor(resolve,reject)
        } catch (err) {
            reject(error)
        }
    }

    then(onFulfilled, onRejected) {
        if (this.status === FULFILLED) {
            onFulfilled(this.value)
        }
        if (this.status === REJECTED) {
            onRejected(this.reason)
        }

        // PENDING说明executor中有异步方法,需要将回掉函数保存起来,等待状态确定后,再依次将对应的函数执行
        if (this.status === PENDING) {
            this.onResolvedCallbacks.push(() => onFulfilled(this.value))
            this.onRejectedCallbacks.push(() => onRejected(this.reason))
        }
    }

}

const promise = new P((resolve,reject) => {
    setTimeout(resolve('success'), 100)
}).then((value)=> {
    console.log('value', value)
}, (reason) => {
    console.log('reason', reason)
})

// 1、定义三种状态
// 2、try executor函数,并捕获错误
// 3、在constructor内定义resolve和reject函数,执行executor函数时候,需要传入两个函数
// 4、在constructor内 定义status、value(保存resolve的结果)、reason(保存rejuct的结果)
// 5、定义then函数,并根据status,去执行onFulfilled或者onRejected,执行的同时,把value or reason 传过去
// 6、兼容executor传入的异步操作。
//   兼容方法:需要先将成功和失败的回调分别存放起来,
//   在executor()的异步任务被执行时,触发 resolve 或 reject,依次调用成功或失败的回调。
console 命令行工具 X clear

                    
>
console