SOURCE

function MyPromise(executor){
    this.state = "pending";
    this.value = null;
    this.onFullfiledFunc = null;
    this.onRejectedFunc = [];

    let resolve = function(value){
        if(this.state === "pending"){
            this.state = "fullfiled";
            this.value = value;
            // console.log('resolve:', this.onFullfiledFunc)

            if(this.onFullfiledFunc){
                let x= this.onFullfiledFunc.call(this, value)
                console.log('x:', x)
            }
        }
    }
    let reject = function(reason){
        if(this.state === "pending"){
            this.state = "rejected";
            console.log('reject')
            this.value = reason;
            if(this.onRejectedFunc){
                this.onRejectedFunc.call(this, reason)
            }
        }
    }

    try{
        executor(resolve.bind(this), reject.bind(this));
    }catch(err){
        reject(err);
    }

}

MyPromise.prototype.then = function(onFullfiled, onRejected){
    // 根据状态调用
    console.log("then:" , this.state)
    let p2 = new MyPromise((resolve, reject)=>{
        if(this.state === "fullfiled"){
            // executor已经执行完了
            try{
                let x = onFullfiled(this.value)
                console.log("!!", x)
                resolve(x)
            }catch(error){
                reject(error)
            }
        }else if(this.state === "rejected"){
            try{
                let x = onRejected(this.value)
                resolve(x)
            }catch(error){
                reject(error)
            }
        }else {
            this.onFullfiledFunc = function(value) {
                let x = onFullfiled(value)
                if(x instanceof MyPromise){
                    console.log('!!!!')
                    x.then((value)=>{
                        console.log('ccc', value)
                        resolve(value)
                    })
                }else{
                    resolve(x)
                }
            }
            this.onRejectedFunc = function(reason) {
                let x = onRejected(reason)
                reject(x)
            }
        }
    })
    return p2;
}


// let p = new MyPromise((resolve, reject)=>{
//     // setTimeout(()=>{
//         console.log(1);
//         reject(3);
//         resolve(2);
//     // }, 0)
// }).then((value)=>{
//     console.log("fullfiled:", value)
// }, (reason)=>{
//     console.log("rejected:", reason)
// }).then(()=>{
//     console.log("fullfiled 2:", value)
// }, ()=>{
//     console.log("rejected 2:", reason)
// })

let p1 = new MyPromise((resolve, reject)=>{
    setTimeout(()=>{
        console.log(1);
        // reject(3);
        resolve(2);
    }, 0)
}).then((value)=>{
    console.log("fullfiled:", value)
    // return new Promise((resolve, reject)=>{
    //     setTimeout(()=>{
    //         resolve(5)
    //     })
    // })
    return new MyPromise((resolve, reject)=>{
        setTimeout(()=>{
        resolve(7)
        })
    })
})

p1.then((value)=>{
    console.log("fullfiled 2:", value)
    return 'xx'
})
console 命令行工具 X clear

                    
>
console