Javascript 在then PROMITE方法之后返回数组变量数据?

Javascript 在then PROMITE方法之后返回数组变量数据?,javascript,promise,geolocation,Javascript,Promise,Geolocation,我创建了一个新的promise函数,将异步地理定位设置为全局函数 function getUsrLoc() { return new Promise(function(resolve, reject) { if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(resolve) } else

我创建了一个新的promise函数,将异步地理定位设置为全局函数

function getUsrLoc() {
return new Promise(function(resolve, reject) {
                    if(navigator.geolocation){
                        navigator.geolocation.getCurrentPosition(resolve)
                    } else {
                        reject('Geolocation is not supported by this browser or OS');
                    }
                });
}
现在,我创建了一个函数,在运行.then方法后,将坐标推送到一个新数组,同时返回带有预期数组项的新变量

//push coordinates into new array

function showPosition(position) {
     var coordinates = new Array();
     coordinates.push(Math.floor(position.coords.latitude))
     coordinates.push(Math.floor(position.coords.latitude))
     console.log(coordinates);
     return coordinates;
}
现在我可以在getUsrLoc()函数上运行.then方法,并插入showPosition函数作为参数

getUsrLoc().then(showPosition);
现在运行时,坐标会打印到控制台(在浏览器提示后),但不会返回新的变量坐标

coordinates;
//undefined

我在这里遗漏了什么?

您不能事先声明变量并期望它具有正确的值。您应该创建一个
然后
处理程序来访问
坐标
变量:

getUsrLoc()
  .then(showPosition)
  .then(function(coordinates) {
    // Do your stuff here
  });

你在哪里印刷?请包括相关代码。
坐标
var仅在
showPosition
函数中声明。。。所以,是的,它在itI之外没有定义。我只是在使用谷歌浏览器控制台。返回坐标;不存储在运行getUsrLoc()之后在控制台中显示的新生成的坐标。然后(showPosition);我看见加洛曼达了,我应该在外面申报吗?如何将其返回到外部以用于其他功能?如果您允许浏览器具有位置访问权限,则它在我的浏览器(FF)中可以完美工作。另外一件事是,我想你的第二个推力应该是经度。在再次阅读你的问题之后。。。天哪?