从诺言中返回数据并访问数据

编程入门 行业动态 更新时间:2024-10-10 17:33:21

从诺言中返回<a href=https://www.elefans.com/category/jswz/34/1771445.html style=数据并访问数据"/>

从诺言中返回数据并访问数据

我有此代码

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处阅读有关承诺的更多信息>

更多推荐

从诺言中返回数据并访问数据

本文发布于:2024-05-07 13:22:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   诺言

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!