数据并访问数据"/>
从诺言中返回数据并访问数据
我有此代码
function getCoords() {
return axios.get(";location=location")
.then((response) => {
let geolocation = [];
geolocation.push(response.data.results[0].locations[0].latLng.lat);
geolocation.push(response.data.results[0].locations[0].latLng.lng);
return geolocation
})
.catch((err) => {
console.log(err)
})
}
console.log(getCoords())
结果,我得到了一个像这样的Promise对象
Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(2)
0: 40.730610
1: -73.935242
length: 2
__proto__: Array(0)
我需要访问坐标数组并将其分配给变量。像这样的东西
let coordinates ;
console.log(coordinates = getCoords())
但是我总是得到一个诺言对象。有没有办法将此信息分配给变量?
提前感谢
回答如下:axios似乎回报了诺言。您必须在诺言完成后登录。
getCoords().then(coords => console.log(coords)).catch(err => console.error(err));
您应该在https://developer.mozilla/en-US/docs/Web/JavaScript/Guide/Using_promises处阅读有关承诺的更多信息>
更多推荐
从诺言中返回数据并访问数据
发布评论