SOURCE

class myPromise {
    static PENDING = 'PENDING';
    static FULFILLED = 'FULFILLED';
    static REJECT = 'REJECT';

    constructor(executor) {
        this.callbacks = [];
        this.status = myPromise.PENDING;
        this.value = null;

        try {
            executor(this.resolve.bind(this), this.reject.bind(this));
        } catch (e) {
            this.reject(e).bind(this);
        }
    }
    resolve(value) {
        if (this.status == myPromise.PENDING) {
            this.status = myPromise.FULFILLED;
            this.value = value;
            this.callbacks.forEach(fn => {

                fn.onfulfilled(value)


            })
        }
    }
    reject(reason) {
        if (this.status == myPromise.PENDING) {
            this.status = myPromise.REJECT;
            this.value = reason;

            this.callbacks.forEach(fn => {


                fn.onrejected(reason)

            })
        }

    }
    then(onfulfilled, onrejected) {
        if (typeof onfulfilled != "function") {
            onfulfilled = () => { }
        }
        if (typeof onrejected != "function") {
            onrejected = () => { }
        }
        if (this.status === myPromise.PENDING) {
            this.callbacks.push({
                onfulfilled: value => {
                    try {
                        console.log(1)
                        onfulfilled(value)
                    } catch (err) {
                        onrejected(err)
                    }
                }
                , onrejected: reason => {
                    try {

                        onrejected(reason)
                    } catch (err) {
                        onrejected(err)

                    }
                }
            }
            )
        }
        if (this.status === myPromise.FULFILLED) {
            setTimeout(() => {
                try {
                    onfulfilled(this.value);
                } catch (err) {
                    onrejected(err)
                }

            })
        } else if (this.status == myPromise.REJECT) {
            setTimeout(() => {
                try {
                    onrejected(this.value)
                } catch (err) {
                    onrejected(err)
                }
            })
        }


    }
}
// let p1 = new myPromise((resolve, reject) => {
//     setTimeout(() => {
//         resolve('解决');
//     }, 1000)
// }).then(value => {
//     console.log(value)
// }, err => {
//     console.log(1)
//     console.log(err)
// })
// console.log(111111111111)

let arr = [];
arr.push({
    a:1,b:2
})
arr.forEach(value=>{
    console.log(value)
})
console 命令行工具 X clear

                    
>
console