SOURCE

function Promise(fn){
  let PENDING="pending";
  let RESOLVED="fulfilled";
  let REJECTED="rejected";
   this.status=PENDING;
   this.value=null;
   this.reason=null;
   this.resolvedCb=[];
  this.rejectedCb=[];
  resolve=function(value){
    setTimeout(()=>{
       this.value=value;
    this.status=PENDING;
    this.resolveCb.forEach(cb=>cb(this.value))
    })
  }
  reject=function(reason){
    setTimeout(()=>{
       this.reason=reason;
    this.status=REJECTED
    this.resolveCb.forEach(cb=>cb(this.reason))
    })
  }
  try{
    fn(resolve,reject)
  }catch(e){
    reject(e)
  }
}

Promise.prototype.then(onFulfilled,onRejected){
  onFulfilled=typeof onFulfilled=="function"?onFulfilled:()=>{}
  onRejected=typeof onRejected=="function"?onRejected:()=>{}
  if(this.status==RESOLVED){
   onFulfilled(this.value)
  }
  if(this.status==REJECTED){
  onRejected(this.reason)
  }
  if(this.status==PENDING){
    this.resolvedCb.push(onFulfilled)
  this.rejectedCb.push(onRejected)
  }
}
let demo=new Promise((resolve,reject)=>{
  resolve(1)
}).then(x=>console.log(x))
console.log(demo)
console 命令行工具 X clear

                    
>
console