SOURCE

// 原生promise

// 新建 MyPromise 类
// Promise.prototype.catch是then(null,rejection)的别名
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
    static resolve(parameter) {
        if (parameter instanceof MyPromise) {
            // 如果传入MyPromise就直接返回
            return parameter;
        }
        return new MyPromise(resolve =>{
            resolve(parameter);
        })
    }
    static reject(reason) {
        return new MyPromise((resolve,reject) => {
            reject(reason);
        })
    }
    constructor(executor) {
        // executor是一个执行器,进入会立即执行executor是一个执行器,进入会立即执行
        try {
            executor(this.resolve,this.reject)
        } catch (error) {
            this.reject(error)
        }
    }

    status = PENDING;
    value = null;
    reason = null;
    onFulFilledCallback = []; // 缓存着,如果程序中有异步则执行这个回调
    onRejectedCallback = []; 

    resolve = (value) => {
        if (this.status === PENDING) {
            this.status = FULFILLED;
            this.value = value;
            while (this.onFulFilledCallback.length) {
                this.onFulFilledCallback.shift()(value)
            }
        }
    }

    reject = (reason) => {
        if (this.status === PENDING) {
            this.status = REJECTED;
            this.reason = reason;
            while (this.onRejectedCallback.length) {
                this.onRejectedCallback.shift()(reason)
            }
        }
    }

    // Promise的then方法,此时未解决异步的问题
    then(onFulFilled = value => value,onRejected = reason => {throw reason}) {
        const promise2 = new MyPromise((resolve,reject) => {
            if (this.status === FULFILLED) {
                queueMicrotask(()=>{
                    try {
                        const x = onFulFilled(this.value);
                        resolvePromise(promise2,x,resolve,reject);
                    } catch (err) {
                        reject(err)
                    }
                })
            } else if (this.status === REJECTED) {
                queueMicrotask(()=>{
                    try {
                        const x = onRejected(this.reason);
                        resolvePromise(promise2,x,resolve,reject)
                    } catch (err) {
                        reject(err)
                    }
                })
            } else if (this.status === PENDING) {
                onFulFilled ? this.onFulFilledCallback.push(onFulFilled):'';
                onRejected ? this.onRejectedCallback.push(onRejected):'';
                this.onFulFilledCallback.push(()=>{
                    queueMicrotask(()=>{
                        try {
                            const x = onFulFilled(this.value)
                            resolvePromise(promise2,x,resolve,reject)
                        } catch (error) {
                            reject(error)
                        }
                    })
                })
                this.onRejectedCallback.push(() => {
                    queueMicrotask(() => {
                        try {
                            const x = onRejected(this.reason);
                            resolvePromise(promise2,x,resolve,reject)
                        } catch (error) {
                            reject(error)
                        }
                    })
                })
            }
        })
        return promise2
    }
}
function resolvePromise(promise2,x,resolve,reject) {
    if (promise2 === x) {
        return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
    }
    if (x instanceof MyPromise) {
        x.then(resolve,reject)
    } else {
        resolve(x)
    }
}
// const promise1 = new MyPromise((resolve,reject) => {
//     setTimeout(function(){
//         resolve('success')
//     },2000)
//     // reject('err')
// });
// promise1.then(value => {
//     console.log('resolve',value)
// },reason => {
//     console.log('reject',reason)
// })

// let promise = new MyPromise((resolve,reject) => {
//     throw new Error('执行器错误')
//     // setTimeout(()=>{
//         // resolve('success')
//     // },2000)
// });
// const p1 = promise.then(value => {
//     return p1
// },reason => {})

// p1.then(value => {
//     console.log(2)
//     console.log('resolve',value)
// },reason => {
//     console.log(3)
//     console.log(reason.message)
// })
// promise.then(res => {
//     console.log(1)
// }).then(res => {    
//     console.log(2)
// })
MyPromise.resolve().then(() =>{
    console.log(0)
})
console 命令行工具 X clear

                    
>
console