javascript - 另一个异步函数中的异步函数
问题描述
我对此感到惊讶,因此希望获得一些关于我在使用 Async Await 语法时做错了什么的指导。
'getUserLocation' 函数返回用户的位置:
getUserLocation = async () => {
const response = await window.navigator.geolocation.getCurrentPosition(
coord => ( coord.coords),
err => {
const { code, msg } = err;
if (code === 1) {
prompt(
"Location is needed to get the local salah time, please try again.",
msg
);
} else if (code === 2 || code === 3) {
prompt(
"Sorry, something went wrong, please try again or try again later.",
msg
);
}
},
{
enableHighAccuracy: true,
timeout: 5000,
}
);
// this.setState({
// userLocation: response,
// });
return response;
};
fetchTimes = async () => {
const { userLocation, method, month, year } = this.state;
const { lattitude, longitude } = userLocation;
if (!lattitude) {
let ress = await this.getUserLocation();
console.log("ress: ", ress);
const url = `http://api.address?latitude=${lattitude}&longitude=${longitude}&method=${method}&month=${month}&year=${year}`;
console.log("url:", url);
const resp = await fetch(url);
....
然而,即使使用 'await' 关键字调用了 'getUserLocation ,控制台仍报告fetch
在提示用户访问位置之前进行了调用,并且该位置返回给 fetchTimes 函数。getUserLocation
为什么在继续呼叫之前不等待先返回位置fetch
?
谢谢
解决方案
推荐阅读
- c# - 如何使用链接按钮在页面之间传递值?
- python-3.x - 在 Python 中处理 XML
- javascript - 如何在 PHP 中将 html 转换为带有图像和所有样式(css)的 word doc 文件?
- sql - 如何在 ORACLE SQL 中打印前三行和后三行?
- java - 休眠搜索 JsonB 索引
- xml - WSO2 REST API 在 WSO2 EI 6.1.1 中捕获无效/有效 xml 请求
- c# - 从 datagridview 导入后关闭 Excel 进程
- python - Python 的 pow() 函数和快速求幂给出了不同的答案
- javascript - 正则表达式仅在 Safari 上失败
- jquery - datatables initComplete 不使用 ajax 源数据