手动实现promise

具体版本应该是通过setTimeout(f,0)来模拟pedding转化状态,下面是为了方便理解跟面试,写了个简略版本的。

function promise () {

  this.status = 'pending' // 2.1

  this.msg = '' // 存储value与reason

  let process = arguments[0],

      that = this

  process (function () {

    that.status = 'resolve'

    that.msg = argument[0]

  }, function () {

    that.status = 'reject'

    that.msg = argument[0]

  })

  return this

}

promise.prototype.then = function () {

  if (this.status === 'resolve') {

    arguments[0](this.msg)

  } else if (this.status === 'reject' && arguments[1]) {

    arguments[1](this.msg)

  }

}

ƒ () {

  if (this.status === 'resolve') {

    arguments[0](this.msg)

  } else if (this.status === 'reject' && arguments[1]) {

    arguments[1](this.msg)

  }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,672评论 0 10
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,124评论 0 2
  • Promise 对象 Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案——回调函...
    neromous阅读 8,812评论 1 56
  • 今天我读了一个有趣的故事。故事讲了一只小螃蟹饿了,见小虾在游水,就追了上去。可它只会横着爬。追起来太费劲。它...
    孙艺冉阅读 1,046评论 0 0
  • 在我妈工作的地方不想写作业时随手画的,不是很好看…… 是的你们随意看看就好(某没人气的作者) (超可爱的分...
    咖妃子阅读 173评论 0 1

友情链接更多精彩内容