手动实现简易的Promise

本文通过手动实现一个简单的Promise,深入解析其工作原理,包括resolve和reject状态转换、then方法的链式调用以及回调函数的执行。通过这个过程,帮助读者更好地理解和运用JavaScript中的Promise。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

const PENDING = 'pending'
const RESOLVED = 'resolved'
const REJECTED = 'rejected'
function MyPromise(fn) {
  const that = this //方便后面利用上下文,先把this赋值给that
  this.state = PENDING // 初始状态为pending
  this.value = null
  this.resolvedCallbacks = [] // 这两个变量用于保存then中的回调,因为执行完Promise时状态可能还是pending
  this.rejectedCallbacks = [] // 此时需要吧then中的回调保存起来方便状态改变时调用
  
  function resolve(value) {
    if(that.state === PENDING) {
      that.state = RESOLVED // resolve函数执行时,若状态为pending,则把状态转换为resolved
      that.value = value // 把值赋给this.value
      that.resolvedCallbacks.map(cb => cb(value)) // 遍历数组,执行之前保存的then的回调函数
    }
  }
  function reject(value) {
    if(that.state === PENDING) { // 同上
      that.state = REJECTED
      that.value = value
      that.rejectedCallbacks.map(cb => cb(value))
    }
  }

  try{
    fn(resolve, reject) // 尝试执行fn函数
  } catch (err) {
    reject(err) // 捕获错误
  }
 }

 MyPromise.prototype.then = function(onFulfilled, onRejected) {
   // 因为then的两个参数均为可选参数,
   // 所以判断参数类型本身是否为函数,如果不是,则需要给一个默认函数如下(方便then不传参数时可以透传)
  // 类似这样: Promise.resolve(4).then().then((value) => console.log(value))
   onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
   onRejected = typeof onRejected === 'function' ? onRejected : err => {throw err}

   if(this.state === PENDING) {
     // 若执行then时仍为pending状态时,添加函数到对应的函数数组
     this.resolvedCallbacks.push(onFulfilled)
     this.rejectedCallbacks.push(onRejected)
   }
   if(this.state === RESOLVED) {
     // resolved状态时,执行onFulfilled
     onFulfilled(this.value)
   }
   if(this.state === REJECTED) {
     // reject状态,执行onRejected
     onRejected(this.value)
   }
 }

// 该例子就是:Promise执行完后,执行then函数时仍为pending状态,所以push函数进resolvedCallbacks后,才执行resolve函数
 new MyPromise((resolve, reject) => {
  setTimeout(() => {
    resolve(1)
  }, 0)
}).then(value => {
  console.log(value)
})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值