promise.all 批量调接口时遇到res为undefined

  • Post author:
  • Post category:其他


  if (+res.status === 200) {
          // console.log(res.data);
          let imgNameGroupList = res.data;

          const allApi = [];
          let urlPrev = "https://hyaiot.net/v1/images/";

          for (let i = 0; i < imgNameGroupList.length; i++) {

            let item = `${urlPrev}${this.data.fileCode}/${imgNameGroupList[i]}`;
            const generateItem = this.generateTemp(item);
            allApi.push(generateItem);
            Promise.all(allApi).then(tempUrlGroup => {
              this.setData({
                images: tempUrlGroup
              });
            });
          }
        
       

          
        }

         generateTemp(url) {
        return new Promise((resolve, reject) => {
          dd.downloadFile({
            url: url,
            headers: {
          "X-Access-Token": this.data.token
        },
        success({ filePath }) {
          console.log(filePath,'transfer successfully!');
        },
        fail(res) {
          console.log(res);
          dd.alert({
            content: res.errorMessage || res.error
          });
        }
      }).then(res => {
        resolve(res.filePath);  
        // 需要用resolve将结果包裹起来这样promise.all 拿到的res才不会为undefined
      });
    });
  },



版权声明:本文为codeLAU原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。