前端之Promise

1.搭建基本结构

是什么?

异步编程的一种解决方案,解决了回调地狱的问题。

为什么要用?

1.支持链式调用,可以解决回调地狱问题。

2.指定回调函数的方式更加灵活

const p = new Promise( (resolve,reject)=>{

} )

p.then(()=>{

},()=>{

})

promise的状态改变

有三个状态:pending,resolved(fulfilled),rejected

状态改变有两种,pending变为resovled或者rejected,状态只能改变一次。

Promise对象的值,PromiseResult

保存着异步任务(成功/失败)的结果。

resolve和reject能访问到这个值并做修改。

流程:

如何改变promise对象的状态:

1.调用resolved函数

2.调用rejected函数

3.抛出错误 throw

promise指定多个成功/失败回调,都会回调吗?

当promise该案对应状态时都会调用。

改变promise状态和指定回调函数谁先谁后?

都有可能,正常情况下都是先指定回调再改变状态,但也可以先改变状态在指定回调。

如何先改变状态在执行回调?

1.在执行器中直接调用resolve/reject函数

2.延迟更长的时间才调用then()

什么时候才能得到数据?

1.如果先指定的回调,那当状态发生改变时,回调函数就会调用得到数据。

2.如果先改变状态,那当指定回调时,回调函数就会调用,得到数据。

promise的异常穿透

当使用promise的then链式调用时,可以在最后指定失败的回调。

前面任何操作出了异常,都会传到最后失败的回调中处理。

中断promise链

 只有返回一个pending状态的promise对象才能中断promise链条

Promise的封装

1.搭建基本结构

function Promise(executor){
}

Promise.prototype.then = function(onreSolvef,onRejected){
}

2.resolve和reject结构

function Promise(executor){
  //resolve函数
  fuction resolve(data){
  }

  //reject函数
  fuction reject(data){
  }
  
  //同步调用exector函数
  exector(resolve,reject);
}

Promise.prototype.then = function(onreSolvef,onRejected){
}

3.resolve和reject代码

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;
  }

  //reject函数
  fuction reject(data){
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;
  }
  
  //同步调用exector函数
  exector(resolve,reject);
}

Promise.prototype.then = function(onreSolvef,onRejected){
}

4.throw抛出异常改变状态

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;
  }

  //reject函数
  fuction reject(data){
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onreSolvef,onRejected){
}

5.状态只能改变一次

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onreSolvef,onRejected){
}

6.then方法中执行回调

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolvef,onRejected){

  //调用回调函数
  if(this.PromiseState === "fulfilled"){
    onResolved(this.PromiseResult);
  }
  if(this.PromiseState === "rejexted"){
    onRejectd(this.PromiseResult);
  }

}

7.异步任务回调的执行

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callback = {};
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    if(self.callback.onResolved)(){
      onResolved(data)
    }
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    if(self.callback.onRejected)(){
      onRejected(data)
    }
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  //调用回调函数
  if(this.PromiseState === "fulfilled"){
    onResolved(this.PromiseResult);
  }
  if(this.PromiseState === "rejected"){
    onRejectd(this.PromiseResult);
  }
  
  //判断pending状态
  if(this.PromiseState === "pending"){
    //保存回调函数
    this.callback = {
      onResolved,
      onRejected
    }
  }

}

8.指定多个回调的实现

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callbacks = [];
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    self.callbacks.forEach(item => {
      item.onResolved(data);
    })
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    self.callbacks.forEach(item => {
      item.onRejected(data);
    })
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  //调用回调函数
  if(this.PromiseState === "fulfilled"){
    onResolved(this.PromiseResult);
  }
  if(this.PromiseState === "rejected"){
    onRejectd(this.PromiseResult);
  }
  
  //判断pending状态
  if(this.PromiseState === "pending"){
    //保存回调函数
    this.callbacks.push({
      onResolved,
      onRejected
    }) 
  }

}

9.同步任务then返回结果

then方法的返回结果是由它的回调函数的执行结果决定的。如果返回了一个非Promise类型数据那resolve()返回的结果就是一个成功的结果;如果返回的是一个Promise,那这个Promise就决定着then方法返回的Promise的结果和状态。

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callbacks = [];
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    self.callbacks.forEach(item => {
      item.onResolved(data);
    })
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    self.callbacks.forEach(item => {
      item.onRejected(data);
    })
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  return new Promise((resolve,reject) =>{
    //调用回调函数
    if(this.PromiseState === "fulfilled"){

      try{
        //获取回调函数的执行结果
        let result = onResolved(this.PromiseResult);
        //判断
        if(result instanceof Promise){
          result.then(v => {
            resolve(v);
          },r => {
            reejct(r);
          })
        }esle{
          resolve(result);
        }
      }catch(e){
        reject(e);
      }
    }
    if(this.PromiseState === "rejected"){
      onRejectd(this.PromiseResult);
    }
  
    //判断pending状态
    if(this.PromiseState === "pending"){
      //保存回调函数
      this.callbacks.push({
        onResolved,
        onRejected
      }) 
    }
  })

}

10.异步任务then返回结果

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callbacks = [];
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    self.callbacks.forEach(item => {
      item.onResolved(data);
    })
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    self.callbacks.forEach(item => {
      item.onRejected(data);
    })
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  const self = this;
  return new Promise((resolve,reject) =>{
    //调用回调函数
    if(this.PromiseState === "fulfilled"){

      try{
        //获取回调函数的执行结果
        let result = onResolved(this.PromiseResult);
        //判断
        if(result instanceof Promise){
          result.then(v => {
            resolve(v);
          },r => {
            reejct(r);
          })
        }esle{
          resolve(result);
        }
      }catch(e){
        reject(e);
      }
    }
    if(this.PromiseState === "rejected"){
      onRejectd(this.PromiseResult);
    }
  
    //判断pending状态
    if(this.PromiseState === "pending"){
      //保存回调函数
      this.callbacks.push({
        onResolved:fuction(){
          try{
            let result = onResolved(self.PromiseResult);
            //判断
            if(result instanceof Promise){
              result.then(v => {
                resolve(v);
              },r => {
                reejct(r);
              })
            }esle{
              resolve(result);
            }
          }catch(e){
            reject(e)
          }
        },
        onRejected:fuction(){
          try{
            let result = onRejected(self.PromiseResult);
            //判断
            if(result instanceof Promise){
              result.then(v => {
                resolve(v);
              },r => {
                reejct(r);
              })
            }esle{
              resolve(result);
            }
          }catch(e){
            reject(e)
          }
        }
        }
      }) 
    }
  })

}

11.代码的补充和完善

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callbacks = [];
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    self.callbacks.forEach(item => {
      item.onResolved(data);
    })
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    self.callbacks.forEach(item => {
      item.onRejected(data);
    })
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  const self = this;
  return new Promise((resolve,reject) =>{
    //调用回调函数
    if(this.PromiseState === "fulfilled"){

      try{
        //获取回调函数的执行结果
        let result = onResolved(this.PromiseResult);
        //判断
        if(result instanceof Promise){
          result.then(v => {
            resolve(v);
          },r => {
            reejct(r);
          })
        }esle{
          resolve(result);
        }
      }catch(e){
        reject(e);
      }
    }
    if(this.PromiseState === "rejected"){
      
       try{
        //获取回调函数的执行结果
        let result = onRejectd(this.PromiseResult);
        //判断
        if(result instanceof Promise){
          result.then(v => {
            resolve(v);
          },r => {
            reejct(r);
          })
        }esle{
          resolve(result);
        }
      }catch(e){
        reject(e);
      }
    }
  
    //判断pending状态
    if(this.PromiseState === "pending"){
      //保存回调函数
      this.callbacks.push({
        onResolved:fuction(){
          try{
            let result = onResolved(self.PromiseResult);
            //判断
            if(result instanceof Promise){
              result.then(v => {
                resolve(v);
              },r => {
                reejct(r);
              })
            }esle{
              resolve(result);
            }
          }catch(e){
            reject(e)
          }
        },
        onRejected:fuction(){
          try{
            let result = onRejected(self.PromiseResult);
            //判断
            if(result instanceof Promise){
              result.then(v => {
                resolve(v);
              },r => {
                reejct(r);
              })
            }esle{
              resolve(result);
            }
          }catch(e){
            reject(e)
          }
        }
        }
      }) 
    }
  })

}

对重复性代码进行封装

function Promise(executor){

  //添加属性
  this.PromiseState = "pending";
  this.PromiseResult = null;

  //
  声明属性
  this.callbacks = [];
  
  //保存实例对象的this值
  const self = this;

  //resolve函数
  fuction resolve(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "fulfilled";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用成功的回调
    self.callbacks.forEach(item => {
      item.onResolved(data);
    })
  }

  //reject函数
  fuction reject(data){

    //判断状态
    if(PromiseState !== "pending") return;
    //修改对象的状态
    self.PromiseState = "rejected";
    //修改对象的结果值
    self.PromiseResult = data;

    //调用失败的回调
    self.callbacks.forEach(item => {
      item.onRejected(data);
    })
  }
  try{
    //同步调用exector函数
    exector(resolve,reject);
  }catch(e){
    reject(e);
  }
}

Promise.prototype.then = function(onResolved,onRejected){

  const self = this;
  return new Promise((resolve,reject) =>{

    //封装
    function callback(type){
      try{
        //获取回调函数的执行结果
        let result = type(self.PromiseResult);
        //判断
        if(result instanceof Promise){
          result.then(v => {
            resolve(v);
          },r => {
            reejct(r);
          })
        }esle{
          resolve(result);
        }
      }catch(e){
        reject(e);
      }
    }
    //调用回调函数
    if(this.PromiseState === "fulfilled"){
      callback(onResolved);
    }
    if(this.PromiseState === "rejected"){
       callback(onRejectd)
    }
  
    //判断pending状态
    if(this.PromiseState === "pending"){
      //保存回调函数
      this.callbacks.push({
        onResolved:callbback(onResolved),
        onRejected:callbback(onRejected)
      }) 
    }
  })

}

11.catch方法和异常穿透

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值