javascript - 异步未正确等待
问题描述
我知道这已经被问过很多次了,但到目前为止我找到的答案似乎都不适用于我的特定问题。我正在尝试创建一个异步函数,该函数还在其中执行一堆 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)
评估条件后运行。
解决方案
推荐阅读
- git - 提交失败后提交更新的 gitignore
- sql - 如何在 SQL 中与 group by 一起使用“select case when”
- javascript - 以编程方式在谷歌散点图中着色点
- python - 创建可变数量的变量并从列表中填充它们
- docker - docker 以“无法检索 runc 版本:fork/exec /usr/bin/runc: exec 格式错误”开始
- django - Django CheckboxSelectMultiple 不允许
- scala - 在 countWord 示例中,我申请了 foreach 但它无法解决符号错误
- angular - 有没有办法在 Zapier 中为 Json Array 创建有效负载?
- java - Firebase:dataSnapshot 上的循环仅执行一次
- react-native - 不变违规:元素类型无效:预期为字符串(用于内置组件)