• 9.9 promise实现 写完了传到gitee上面了,这里这个不完整


    //promise:then,catch,finally,all,race
    
    // then时state还是pending等待状态 我们就需要在then调用的时候,将成功和失败存到各自的数组,一旦reject或者resolve,就调用它们,类似发布订阅
    
    //pending,fulfilled,rejected
    const pedding = 'pedding'
    const fulfilled = 'fulfilled'
    const rejected = 'rejected'
    class Promise {
      constructor(executor) {
        console.log('executor:'+ executor)
       this.status = pedding //初始状态
       this.value = undefined //成功或者失败的值
       this.fail = undefined
       this.successCallback = [] //成功或者失败的回调函数
       this.errorCallback = []
       let resolve = (value) => { //成功之后就依次执行
       setTimeout(() => {
        if (this.status === pedding) {//只有在状态是pending的时候才能改变
          //console.log('resolve:'+value)
           this.status = fulfilled
           this.value = value
           this.successCallback.forEach(fn => {
             fn()
           })
         }  
       }, 0);
       };
       let reject = (fail) => {
         setTimeout(() => {
          if (this.status === pedding) {
          //  console.log('reject:'+ fail)
            this.status = rejected
            this.fail = fail
            this.errorCallback.forEach(fn => {
              fn()
            })
           }
         }, 0);
       };
       try {
        executor(resolve,reject)
       }catch(err){
        reject(err)
       }
          
      }
        then(success,error) {
        if(this.status === fulfilled) {
         return new Promise((resolve,reject) => {
          try{
            let fun = success(this.value)
            if(fun instanceof Promise) {
              fun.then(resolve,reject)
            }else{
              resolve(fun)
            }
          }catch(err) {
              reject(err)
          }
         })
        }
        if(this.status === rejected) {
          return new Promise((resolve, reject) => {
    	      try {
    	        let j = error(this.fail)
    	        if (j instanceof Promise) {
    	          j.then(resolve, reject)
    	        } else {
    	          resolve(j)
    	        }
    	      } catch (err) {
    	        reject(err)
    	      }
    	    })
        }
        // console.log(this.reject)
        // 订阅成功失败
        if(this.status === pedding) {//刚进来必须是pedding状态,否则状态一旦变了回调还没有注册
          return new Promise((resolve, reject) => {
            this.successCallback.push(() => {
              let f = success(this.value)
    	        if (f instanceof Promise) {
    	          f.then(resolve, reject)
    	        } else {
    	          resolve(f)
    	        }
            })
            this.errorCallback.push(() =>{
             
              let j = error(this.fail)
              if (j instanceof Promise) {
                j.then(resolve, reject)
              } else {
                resolve(j)
              }
            })
          })
         
        }
       }
      
    }
    let  p = new  Promise((resolve,reject) =>{
       //reject('失败')
       resolve('成功1')
    })
    
     p.then((res) => {
       console.log('then的第一个参数成功'+res)
       return new  Promise((resolve,reject) =>{
        //reject('失败')
        resolve('成功2')
     })
     },()=>{
    console.log('失败')
     }).then((res) => {
      console.log('then的第二个参数成功'+res)
    },()=>{
    console.log('失败2')
    })
    // .then((res) => {
    //   console.log('then的第3个参数成功'+res)
    // },()=>{
    // console.log('失败3')
    // })
    // p.then()
    //直接在then后面then 报错Cannot read property 'then' of undefined,所以需要返回一个新的promise对象
    

      

    输出
    
    executor:(resolve,reject) =>{
       //reject('失败')
       resolve('成功1')
    }
    executor:(resolve, reject) => {
            this.successCallback.push(() => {
              let f = success(this.value)
    	        if (f instanceof Promise) {
    	          f.then(resolve, reject)
    	        } else {
    	          resolve(f)
    	        }
            })
            this.errorCallback.push(() =>{
             
              let j = error(this.fail)
              if (j instanceof Promise) {
                j.then(resolve, reject)
              } else {
                resolve(j)
              }
            })
          }
    executor:(resolve, reject) => {
            this.successCallback.push(() => {
              let f = success(this.value)
    	        if (f instanceof Promise) {
    	          f.then(resolve, reject)
    	        } else {
    	          resolve(f)
    	        }
            })
            this.errorCallback.push(() =>{
             
              let j = error(this.fail)
              if (j instanceof Promise) {
                j.then(resolve, reject)
              } else {
                resolve(j)
              }
            })
          }
    then的第一个参数成功成功1
    executor:(resolve,reject) =>{
        //reject('失败')
        resolve('成功2')
     }
    executor:(resolve, reject) => {
            this.successCallback.push(() => {
              let f = success(this.value)
    	        if (f instanceof Promise) {
    	          f.then(resolve, reject)
    	        } else {
    	          resolve(f)
    	        }
            })
            this.errorCallback.push(() =>{
             
              let j = error(this.fail)
              if (j instanceof Promise) {
                j.then(resolve, reject)
              } else {
                resolve(j)
              }
            })
          }
    then的第二个参数成功成功2
    
    [Done] exited with code=0 in 0.095 seconds
    

      

  • 相关阅读:
    竖版文字排列实现《金刚般若波罗蜜心经》
    前端气泡效果实现的方式
    纯CSS绘制三角形
    什么是块级格式上下文
    绝对定位元素left、right、top、bottom值与其margin和宽高的关系
    currentColor在CSS的含义
    HTML/css清除浮动的几种方式
    W3C中不同标准的含义
    table表格标签的属性
    输入你的生日某年某月某日,判断这一天是这一年的第几天、星期几?
  • 原文地址:https://www.cnblogs.com/zjj-study/p/13639593.html
Copyright © 2020-2023  润新知