SOURCE

function MyPromise(executor){
  var self = this;
  self.status = 'pending',
    self.value,
    self.reason;
  self.onResolvedCallbacks = [];
  self.onRejectedCallbacks = [];
  
  
  function resolve(value) {
    self.status = 'resolved';
    self.value = value;
    console.error(value)
    self.onResolvedCallbacks.forEach(function(fn){fn(value)})
  }
  function reject(reason) {
    self.status = 'rejected';
    self.reason = reason;
    self.onRejectedCallbacks.forEach(function(fn){fn(reason)})
  }
  executor(resolve, reject)
}

MyPromise.prototype.then = function(onFulfilled, onRejected){
  var self = this;
  if (this.status == 'resolved') {
    onFufilled(this.value);
  }
  if (this.status == 'rejected') {
    onRejected(this.reason);
  }
  console.log(self.status === 'pending')
  if (self.status === 'pending') {
        self.onResolvedCallbacks.push(onFulfilled);
        self.onRejectedCallbacks.push(onRejected);
    }
  console.log(self.onResolvedCallbacks);
}

var mp = new MyPromise(function(resolve,reject){
  setTimeout(function(){
    resolve(1);
  },1000)
});
mp.then(function(e){
  console.log(e)
})
console 命令行工具 X clear

                    
>
console