异步未正确等待

时间:2019-08-03 09:25:50

标签: javascript asynchronous

我知道有人问过很多次,但是到目前为止,我发现的答案似乎都不适用于我的特定问题。我正在尝试创建一个异步函数,该函数还可以在其中执行一堆axios调用,然后一旦它们全部完成,它就会执行其他操作。

我以为将所有内容包装在异步函数中,然后等待可以解决问题,但这似乎没有用。

let gamesFound = 0;

  mainFn();

  async function mainFn() {
    await loader();
    if (gamesFound == 0) {
      document.getElementById("indexH1").innerHTML = "Oops! We didn't find any data.";
      document.getElementById("introLine").innerHTML = "It doesn't look like there are any games with that time control in the date range, please try changing your filters.";
      document.getElementById("loadingCheck").style.display = "none";
    }
  }

  async function loader() {

    axios.get(url1)
      .then((response) => {
        // Some synchronous code with the response
        axios.get(url1)
          .then((response) => {
            // Some more synchronous code with the response of this
              axios.get(archive)
                .then((response) => {
                  gamesFound++;
                .catch((error) => {
                })
            }
          })
          .catch((error) => {
          })
      })
      .catch((error) => {
      })

  }

第一部分似乎运行良好,但实际上loader()似乎在评估了if (gamesFound ==0)条件之后运行。

0 个答案:

没有答案