javascript - 将 promise 的内容保存到外部数组
问题描述
当我尝试在 之外的数组中捕获 Promise 的值时.then()
,该数组被填充undefined
(尽管console.log()
记录了正确的值)。
async function fetchCoordinates(pluscode) {
let response = await fetch(url);
return await response.json();
}
let places = []
fetchCoordinates(pluscode)
.then(geoinfo => {
console.log(geoinfo)
console.log(geoinfo.plus_code.geometry.location.lat)
places.push(
{position: new google.maps.LatLng(
geoinfo.plus_code.geometry.location.lat,
geoinfo.plus_code.geometry.location.lng
)}
)
})
解决方案
推荐阅读
- c# - Sendgrid API 返回 Forbidden C#
- react-native - 世博会:找不到变量要求
- c# - 如何使用动态到正常模型?
- linux - 尝试杀死正在运行的python脚本时“没有这样的进程”
- java - org.postgresql.util.PSQLException 在 DefaultLockRegistry#acquire 期间“违反唯一约束”
- url - 从 MS Word 的目录中清除 URL 链接
- java - 截屏并将图像显示到图库中
- haskell - 如何用“反射”包替换 ImplicitParams?
- reactjs - 错误无法读取未定义的属性“匹配”
- linux - 对主要命令输出的所有 PID 执行命令?