JS之路:Promise

手动实现一个简易版的Promise

function PromiseSimple (excutor) {
      let _this = this
      let _status = 'pending'
      let successCallBack = undefined
      let failCallBack = undefined
      excutor(resolve.bind(_this), reject.bind(_this))
      function resovle (params) {
        if (_this._status === 'pending') {
          _this._status = 'full'
          _this.successCallBack(params)
        }
      }
      function reject (params) {
        if (_this._status === 'pending') {
          _this._status = 'fail'
          _this.failCallBack(params)
        }
      }
 }
 PromiseSimple.prototype.then = function (full, fail) {
      this.successCallBack = full
      this.failCallBack = fail
    }
posted @ 2020-12-17 09:54  无所从来,亦无所去  阅读(71)  评论(0)    收藏  举报