美文网首页
手写 promise

手写 promise

作者: 月半女那 | 来源:发表于2023-01-29 11:40 被阅读0次
    function MyPromise(exctor) {
      this.state = 'pending';
      this.fillFulledResult = ''
      this.rejectReason = ''
      this.fullfilledList = []
      this.rejectList = []
      const  reslove =  (value) => {
        if (this.state == 'pending') {
          this.state = 'fullfilled'
          this.fillFulledResult = value
          this.fullfilledList.forEach((fn) => {
            fn(value)
          })
        }
      }
      const  reject = (value) => {
        if (this.state == 'pending') {
          this.state = 'rejected'
          this.rejectReason = value
          this.rejectList.forEach((fn) => {
            fn(value)
          })
        }
      }
      exctor(reslove, reject)
    }
    
    // promise是链式调用的,所以在then 方法里返回的是一个promise对象
    MyPromise.prototype.then = function (onFullfilled, onReject) {
      let that = this;
      return new MyPromise((reslove, reject) => {
        switch (that.state) {
          case 'pending':
            that.fullfilledList.push(value => { 
              setTimeout(() => {
                try {
                  const result = onFullfilled(value);
                  reslovePromiseHandle(result)
              } catch (error) {
                reject(error)
              }
              }, 0);
            })
            that.rejectList.push(value => { 
              setTimeout(() => {
                try {
                  const result = onReject(value);
                  reslovePromiseHandle(result)
                } catch (error) {
                  reject(error)
                }
              }, 0);
            })
            break
          case 'fullfilled':
            setTimeout(() => {
              try {
                const success = onFullfilled(that.fillFulledResult);
                reslovePromiseHandle(success)
              } catch (error) {
                reject(error)
              }
            }, 0);
          
          
            break
          case 'rejected':
            setTimeout(() => {
              try {
                const errorreason = onReject(that.rejectReason);
                reslovePromiseHandle(errorreason)
              } catch (error) {
                reject(error)
              }
            }, 0);
            break
        }
        function reslovePromiseHandle (result) { 
          if (result instanceof MyPromise) { 
            result.then(reslovePromiseHandle, reject)
            return 
          }
          reslove(result)
        }
      })
    }
    
    MyPromise.prototype.catch = function (onReject) { 
      return this.then(null , onReject)
    }
    
    
    new MyPromise((reslove, reject) => { 
      reslove(2)
    }).then(res => { 
      console.log('+++',res)
      return res +'1111'
    }).then(res => new MyPromise((reslove, reject) => { 
      console.log(res +'1111')
      reject('error' + res)
    })).catch(error => { 
      console.log(error)
      return 'jhhhh'
    }).then(res => { 
      console.log(res +'1111+catch')
    })
    //+++ 2
    //211111111
    //error21111
    //jhhhh1111+catch
    

    相关文章

      网友评论

          本文标题:手写 promise

          本文链接:https://www.haomeiwen.com/subject/wafphdtx.html