一尘不染

axios.get 第一次加载不正确,但可以刷新

javascript

我已经尝试了一切,以便在第一次加载时加载。我什至添加了 .finally 作为绝望的措施。我尝试了异步等待,但不确定我是否正确地做到了。这是有效的版本,但我必须刷新页面,因为它第一次加载不正确。我需要这是香草 javascript。

axios.get(`https://api.github.com/repos/${orgEl}/${repoEl}/contents/Transactions/${projectJ}/${fundJ}/${poolJ}`)
      .then(response => {
        const data = response.data;
        console.log('data', data);          
        for (let j in data) {
          budgetI[j] = data[j].name.replace(/\s/g, '-'); //* over here is the 'budgetI' variable
          axios.get(`https://api.github.com/repos/${orgEl}/${repoEl}/contents/Transactions/${projectJ}/${fundJ}/${poolJ}/${budgetI[j]}`)
          .then(response => {
            const data = response.data;
            for (let m in data) {    
              axios.get(data[m].download_url)
              .then(response => {
                const data = response.data;
                bi.push(data);  //* over here is the 'bi' variable
              })
              .catch(error => console.error(error)).finally(() => {
                console.log("first load");
            });
            }      
          })
          .catch(error => console.error(error)).finally(() => {
            console.log("mid load");
        });   
          }          
      })
      .catch(error => console.error(error))
      .finally(() => {
        console.log("final load");
    });

axios.get(`https://pool.pm/wallet/${walletEl}`)
      .then(response => {
        for (let i in bi) {
          y = bi[i].budget.replace(/\s/g, '-')   //* bi value comes from axios.get above
          for (let j in budgetI) {    //* budgetI value comes from axios.get above
            if ( y == budgetI[j]) {
              totals[y] = totals[y] + (parseInt(bi[i].ada));
              totals.outgoing = totals.outgoing + (parseInt(bi[i].ada));
            }        
          }
        };

最后一个 axio.get 使用顶部 axios.get 中的“budgetI”和“bi”变量

当我输入这个时,我意识到也许我应该 promise.all 第一个 axios.get 和最后一个 axios.get 。我会尝试,但无论如何都会上传。希望有人可以向我展示一种改进此代码的简单方法


阅读 103

收藏
2022-07-25

共1个答案

一尘不染

在不了解您的程序或那些 GitHub 存储库的结构的情况下,我将其分解为两个async函数:

async function downloadFromDownloadURLs(url) {
  console.log(`==> ${url}`);
  const { data } = await axios.get(url);
  const downloadedData = [];
  for (let key in data) {
    let downloadUrl = data[key].download_url;
    console.log(`--> ${url}`);
    const downloadResponse = await axios.get(downloadUrl);
    downloadedData.push(downloadResponse.data);
  }
  return downloadedData;
}

async function loadData(orgEl, repoEl, projectJ, fundJ, poolJ) {
  let prefixUrl = `https://api.github.com/repos/${orgEl}/${repoEl}/contents/Transactions/${projectJ}/${fundJ}/${poolJ}`;
  const { data } = await axios.get(prefixUrl);
  const allDownloadedData = [];
  for (let dataKey in data) {
    const budget = data[dataKey].name.replace(/\s/g, "-");
    const url = `${prefixUrl}/${budget}`;
    const budgetData = await downloadFromDownloadURLs(url);
    allDownloadedData.push([budget, budgetData]);
  }
  return allDownloadedData;
}
2022-07-25