循环请求接口,统一处理

在实际开发中,碰到参数不同,接口相同,需要发送多个请求拿到数据后统一做处理的情况

 

async login() {
    const datas = ["小明", "小红", "小李"]; // 循环体
    const promise = datas.map(i => {
    const url = "http://xxx.x.x.x/login?user=" + i;
    return new Promise((resolve, reject) => {
        axios
        .get(url)
        .then(res => resolve(res))
        .catch(err => reject(err)); // 接口
    });
    });
    const { data } = await Promise.all(promise);
    if (data) console.log(data); // 结果参数的和,处理结果
},

 

有的时候要另外请求列表的图片,需要循环列表

    {{ImgMap[row.id]}}

    loadImgs () {
      const map = {}
      this.lists.forEach(element => {
        const id = item.id
        if (!this.ImgMap[id]) {
          api({ id: id }).then(res => {
            map[id] = res.data || id
            this.ImgMap = { ...this.ImgMap, ...map }
          }).catch(() => {
            this.ImgMap[id] = id
          })
        }
      });
    },

 



posted on 2021-09-15 10:09  sss大辉  阅读(439)  评论(0编辑  收藏  举报

导航