Javascript 找回内在承诺的价值

Javascript 找回内在承诺的价值,javascript,angularjs,location,promise,angular-promise,Javascript,Angularjs,Location,Promise,Angular Promise,我试图从控制器访问嵌套承诺的值 这是我的控制器。我正在呼叫我的服务,希望返回城市名称: LocationService.getCurrentCity(function(response) { // This is never executed console.log('City name retrieved'); console.log(response); }); 这是服务。我正在更新客户的位置,然后我向谷歌请求城市信息。console.log(城市)按预期记录正确的

我试图从控制器访问嵌套承诺的值

这是我的控制器。我正在呼叫我的服务,希望返回城市名称:

LocationService.getCurrentCity(function(response) {
    // This is never executed
    console.log('City name retrieved');
    console.log(response);
});
这是服务。我正在更新客户的位置,然后我向谷歌请求城市信息。
console.log(城市)
按预期记录正确的城市

this.getCurrentCity = function() {
    return this.updateMyPosition().then(function() {
        return $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + myPosition.lat + ','+ myPosition.lng +'&sensor=false').then(function(response) {
            var city = response.data['results'][0]['address_components'][3]['long_name'];
            console.log(city);
            return city;
        });
    });
}

我如何在我的控制器中访问城市?

您正在返回一个承诺,应该用
打开它,然后

LocationService.getCurrentCity().then(function(response) {
    // This is never executed
    console.log('City name retrieved');
    console.log(response);
});

承诺通过使用返回值来工作,就像使用同步值一样-当您调用
getCurrentCity
时,它返回一个承诺,您可以使用
然后

@Maeh来打开它-不强制函数使用的参数数量是JS的一个恼人属性。一个好的静态分析工具在发现这些恼人的bug方面真的可以走很长的路。