mpvue结合axios使用Promise.all()

2019-09-28  本文已影响0人  嗷呜的伍

上代码

methods:{
  promise(){
      let that = this;
      //p1为promise实例
      let p1 = new Promise(async(resolve,reject)=>{
          let params = {
            params:JSON.stringify({token:mpvue.getStorageSync('token')})
          }
          let res = await codePicInterface(params); //接口A
          //res存在则请求接口成功         
          if (res) {
            return resolve(res)
          }
        })
      //p2为函数
      let p2 = (type) =>{
        return new Promise(async(resolve,reject)=>{ //接口B
          let res = await swiperInterface({type});
          //res存在则请求接口成功   
          if (res) {
            return resolve(res)
          }
        })
      }
      Promise.all([p1,p2('home')]).then((res)=>{
      //res为数组,返回两个接口的请求接口
        console.log(res); 
      }).catch(error=>console.error(error))
    },
    // 获取图片信息
    getImageInfo() {
      let that = this;
      let promise = (src)=>{
        return new Promise(function (resolve, reject) {
          mpvue.getImageInfo({
            src: src,
            success(res) {
              return resolve(res)
            },
            fail(res) {
              return reject(res)
            }
          });
        });
      }

      let p1 = promise('https://dmsdev.aosmith.com.cn/api/baseServer/fileUploadService/transactionFile/2813?token=184742890506870268874c2aa');
      let p2 = promise('/static/images/tempImg/code.png')
      Promise.all([p1, p2]).then((res)=>{
        console.log(res);
      }).catch((res)=>{
        console.log(res);
      });
    },
}

关于代码中的请求接口使用方法参照我之前写的文章mpvue之axios请求封装

上一篇下一篇

猜你喜欢

热点阅读