SOURCE

class MyPromise{
    constructor(fn){
        this.state = "pending";
        this.val = "";
        this.onResolvedcb=[];
        this.onRejectedcn=[];
        fn(this.resolve.bind(this),this.reject.bind(this));
    }
    resolve(value){
        if(this.state =='pending'){
            this.state= 'fulfilled';
            this.val = value;
            this.onResolvedcb.map(cb=>cb())
        }
    }
    reject(value){
        if(this.state =='pending'){
            this.state= 'rejected';
            this.val = value;
            this.onRejectedcn.map(cb=>cb())
        }
    }
    then(resolveFunc,rejectFunc){
        //console.log(this.state,'this.state')
        if( this.state =="pending"){
            this.onResolvedcb.push(resolveFunc);
            this.onRejectedcn.push(rejectFunc);
        }
        if( this.state =="fulfilled"){
            resolveFunc(this.val)
        }
        if( this.state =="rejected"){
            rejectFunc(this.val)
        }
    }
}
let p1 = new MyPromise((resolve,reject)=>{
    console.log('1');
    resolve('success');
    console.log('2')
})
p1.then((vale)=>{
    console.log(vale)},(vale)=>{
    console.log(vale)})
console 命令行工具 X clear

                    
>
console