javascript - Wait for an Axios call to finish in Quasar / Vue
问题描述
I have a function in a Quasar application that needs to call an Web API that I need to wait until I get the results back to call the next function. I've posted my code and I'm sure that I'm missing some await or async or have them in the wrong place.
Global ex='';
testFunction3(arg){
console.log(arg)
} ,
testFunction2(){
this.ex = update_Members_data_term("@musc.edu");
},
async testFunction(){
await this.testFunction2()
this.testFunction3(this.ex)
},
API call:
function update_Members_data_term(term) {
axios.get(
'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi', {
params: {
db: 'pubmed',
api_key: '',
retmode: 'json',
retmax: 200,
term: term
}
}
).then(async response => {
return response.data.esearchresult.idlist;
}).catch(error => {
console.log(error.response)
})
}
Thanks for the help.
解决方案
testFunction2
不返回 aPromise
所以await
什么都不做。你实际上在做await null;
所以修复将是:
function update_Members_data_term(term) {
return new Promise((resolve,reject) => {
axios.get(
'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi', {
params: {
db: 'pubmed',
api_key: '',
retmode: 'json',
retmax: 200,
term: term
}
}
).then(async response => {
resolve(response.data.esearchresult.idlist);
}).catch(error => {
reject(error.response)
})
});
}
async testFunction2(){
this.ex = await update_Members_data_term("email_here");
},
- 请注意,您可能只是
await axios.get
不返回 Promise,但我不确定语法是什么。
推荐阅读
- graph-algorithm - 这个 LCA 算法可以吗,在其他地方使用过吗?
- sql - 尝试查找有行和无行的视图
- docker - 如何使用 jre-alpine 基础镜像创建 Scala Play 应用程序的 Docker 镜像
- google-chrome - Web 蓝牙特性特征值在手机上改变
- python - 我的 kivy 程序总是旋转 180 度并颠倒过来
- mongodb - dateFromString 不涵盖有效日期
- spring - 如果 Spring kafka 无法连接到代理,请不要停止应用程序
- node.js - 如何正确处理 Node.js gRPC 客户端和服务器之间的回调
- reactjs - 使用特定的 .env 文件运行 npm build
- bash - 通过远程 WSL 扩展在 vscode 中使用 WSL bash 与在 Windows 中选择作为默认 shell 使用它有何不同?