new promise 以及promise.all在项目中的实际应用

需求:在实际项目的开发应用中我们需要先执行前两个方法,用两个方法返回的数据作为参数去执行第三个方法
这时候我们就需要使用new promise、resolve、promise.all等方法

  • 使用代码如下所示:可根据实际情况进行变通
  • 方法1
// 账户信息获取
getAccount(){
  return new Promise((resolve,reject)=>{
    this.$axios.post(CONFIG.httphost() + '/user/queryUser',{
    st:1,
    limit:0
  }).then(resp =>{
    let res = resp.data;
    let code = res.code;
    if(!code){
      this.accountList = res.data;      
      //值得注意的是这里,new promise的方法正确请求是要resolve出来,才能继续向下执行
      resolve();
    }
  })
  })
},
  • 方法2
// 获取单位
getDW() {
  return new Promise((resolve,reject)=>{
    this.$axios.post(CONFIG.httphost() + "/MessageShowController/showDwMessage", {
      st: 1,
      limit: 0
    }).then((resp) => {
      let res = resp.data;
      let code = res.code;
      if (!code) {
        this.dwList = res.data;        
        resolve()
      } else {
        this.giveMsg("请求数据失败");
      }
    });
  })      
},
  • 方法3
// 获取数据
 getData() {
   Promise.all([
     this.getAccount(),
     this.getDW(),
   ]).then(res=>{
   //此处的请求方法根据实际封装变通
   this.$axios.post(CONFIG.httphost() + "/MessageCollectorController/showBmJurisdiction", {
       dw_id: self.dw_id,
       userid:this.userid
     })
     .then((resp) => {
       let res = resp.data;
       let code = res.code;
       if (!code) {
         this.$nextTick(() => {
          this.getRoleMenu(res.data);
         }); 
       }
     })
     .catch((err) => {
     });
   })      
 },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值