手写一个Promise,附源码分析

手写promise

话不多说,直接上代码了

 const PENDING = 'pending'
    const RESOLVED = 'resolved'
    const REJECTED = 'rejected'
    //首先创建三个常量用于表示promise的三个状态
    function MyPromise(fn){
        const that = this   //在函数体内部首先创建常量that,因为代码可能会异步执行,用于获取正确的this对象
        that.state = PENDING   //一开始Promise的状态是 pending  
        that.value = null  //value 变量用于保存resolve或者reject中传入的值
        that.resolvedCallbacks = []
        that.rejectedCallbacks = []
      
      // resolvedCallbacks和rejectedCallbacks,用于保存then中的回调,因为当执行完Promise时状态可能还是等待中,这时候应该把then中的回调保存起来用于状态改变时使用
      
        function resolve(value) {
            if(that.state === PENDING) {
            //首先两个函数都得判断当前状态是否为等待中,因为规范规定只有等待态才可以改变状态
                that.state = RESOLVED
                that.value = value
                //将当前状态更改为对应状态,并且将传入的值赋值给 value
                that.resolvedCallbacks.map(cb => cb(that.value))
                //遍历回调数组并执行
            }
        }

        function reject(value) {
            if(that.state === PENDING){
                that.state = REJECTED
                that.value = value;
                that.rejectedCallbacks.map(cb => cb(that.value));
            }
        }
        try {
            fn(resolve, reject)
        } catch (e) {
            reject(e)
        }
        //执行promise中传入的函数,执行传入的参数并且将之前两个函数当做参数传进去
要注意的是,可能执行函数过程中会遇到错误,需要捕获错误并且执行 reject 函数
    }
复制代码

实现then函数

then函数是在Promise构造器中成功状态下调用的resolve方法的回调。

then函数是可以接收两个参数的,一个是用户自定义的成功处理,另一个是用户自定义的错误处理,第二个参数可不传。

MyPromise.prototype.then = function(onFulfilled, onRejected) {
        const that = this
        //对传入的两个参数做判断,如果不是函数将其转为函数
        onFulfilled =
            typeof onFulfilled === 'function'
                ? onFulfilled
                : v => v  
                // onFulfilled = v => v
        onRejected =
            typeof onRejected === 'function'
                ? onRejected
                : r => {
                    throw r
                }

        if(that.state === PENDING) {
            that.resolvedCallbacks.push(onFulfilled)
            that.rejectedCallbacks.push(onRejected)
        }
        else if(that.state === RESOLVED) {
            onFulfilled(that.value)
        }
        else {
            onRejected(that.value)
        }
    }

    new MyPromise((resolve, reject) => {
        setTimeout(() => {
              // resolve('成功的回调数据')
            reject('失败')
        }, 1000)
    }).then((res)=>{
        console.log(res)
    },(rej)=>{
        console.log(rej)
    })

复制代码

代码无注释 清爽版

const PENDING = 'pending'
const RESOLVED = 'resolved'
const REJECTED = 'rejected'

function MyPromise(fn){
  const that = this
  that.state = PENDING
  that.value = null
  that.resolvedCallbacks = []
  that.rejectedCallbacks = []
  
  function resolve(value) {
    if(that.state === PENDING) {
      that.state = RESOLVED
      that.value = value
      that.resolvedCallbacks.map(cb => cb(that.value))
    }
  }
  
  function reject(value) {
    if(that.state === PENDING){
      that.state = REJECTED
      that.value = value;
      that.rejectedCallbacks.map(cb => cb(that.value));
    }
  }
  try {
    fn(resolve, reject)
  } catch (e) {
    reject(e)
  }
}

MyPromise.prototype.then = function(onFulfilled, onRejected) {
  const that = this
  //对传入的两个参数做判断,如果不是函数将其转为函数
  onFulfilled = 
    typeof onFulfilled === 'function'
    ? onFulfilled 
    : v => v  // onFulfilled = v => v
  onRejected = 
    typeof onRejected === 'function'
    ? onRejected
    : r => {
      throw r
    }
  
  if(that.state === PENDING) {
    that.resolvedCallbacks.push(onFulfilled)
    that.rejectedCallbacks.push(onRejected)
  }
  else if(that.state === RESOLVED) {
    onFulfilled(that.value)
  }
  else {
    onRejected(that.value)
  }
}

new MyPromise((resolve, reject) => {
 setTimeout(() => {
              // resolve('成功的回调数据')
            reject('失败')
        }, 1000)
    }).then((res)=>{
        console.log(res)
    },(rej)=>{
        console.log(rej)
    })
复制代码

转载于:https://juejin.im/post/5c8c5836e51d452f1c7e5348

你可能感兴趣的:(手写一个Promise,附源码分析)