javascript - How do I turn a javascript ajax caller function into async/await?
问题描述
Let's say I have the following function getData
getData = (param) => {
// jquery
$.ajax(...).done(..)
}
How do I turn getData
into a blocking function so that refreshPage
only gets called after the ajax within getData
is done?
data = getData(..)
refreshPage()
Please note: I don't want to pass refreshPage
into the ajax callback within getData
OK... here is what I tried and seems working:
getData = (param) => {
// jquery
return $.ajax(...).done(..)
}
getData.then(refreshPage())
解决方案
function getData = (param) => {
// jquery
return $.ajax(...);
}
async test() {
var x = await getData()
console.log(x)
}
推荐阅读
- angular - 我是 Angular 的新手,并试图在每个(onchange)事件之后获取输入并将此输入发送到 set 方法。见下面的代码
- c# - 如果在 foxpro 中,IIF 功能是否被重新定义?
- scala - 如何通过检查不正确的字符串模式来编写测试以进行解析
- reactjs - 放置 React 错误边界组件的理想位置?
- java - 尝试发出简单的 Http GET 请求时连接超时
- node.js - 我想用 Nodejs 在浏览器中打开一个文件(pdf、docx)
- python - 如何使用框架熊猫将csv文件中的列添加到数组中
- java - 当我在 Champion.json 中有特定行的“键”时,从 JSON 文件中获取“id”
- sql - 检查连接字符串数据的 SQL 外键
- vb.net - 与其他项目共享模块中的常量