AngularJS Service不等待其他服务完成

时间:2015-05-27 03:06:52

标签: angularjs angularjs-service angular-http

当服务正在调用服务时,我遇到了问题。似乎事情正在按顺序执行。

.service('addressService', function($http) {
    var addressService = this;
    addressService.getLatitudeLongitude = function(address) {
        console.log('in addressService.getLatitudeLongitude')
        $http.get('/location?address=' + encodeURIComponent(address))
        .success(function(data, status, headers, config) {
            console.log('success')
            console.log(data);
            return data;
        })
        .error(function(data, status, headers,config) {
            console.log('error')
        });
    };
})

.service('weatherService', function($http, addressService){
    var weatherService = this;

    weatherService.getWeather = function(latitude, longitude) {
        console.log('get weather')
        $http.get('/all_weather?latitude=' + latitude + '&longitude=' + longitude)
        .success(function(data, status, headers, config) {
        return data;
        })
        .error(function(data, status, headers, config) {
            return null;
        });
    };

    weatherService.getCurrentWeather = function(address) {
        var lat_lon = addressService.getLatitudeLongitude(address) 
        console.log(lat_lon)
        return weatherService.getWeather(lat_lon.lat, lat_lon.lng).currently;
    };
})

输出结果为:

weather.js:25 getCurrentWeatherByAddress 
weather.js:99 in addressService.getLatitudeLongitude
weather.js:129 undefined
angular.js:11655 TypeError: Cannot read property 'lat' of undefined
    at weatherService.getCurrentWeather (weather.js:130)
    at weather.getCurrentWeatherByAddress (weather.js:28)
    at ib.functionCall (angular.js:12404)
    at hint.js:798
    at n.$get.n.$eval (angular.js:14466)
    at n.$get.n.$apply (angular.js:14565)
    at n.scopePrototype.$apply (hint.js:1478)
    at HTMLButtonElement.<anonymous> (hint.js:797)
    at HTMLButtonElement.c (angular.js:3032)
weather.js:102 success
weather.js:103 Object {lat: 44.8735964, lng: -93.2835137}

weatherService.getCurrentWeather(address)完成请求之前,addressService.getLatitudeLongitude(address)似乎试图继续addressService.getLatitudeLongitude(address)。发生weatherService.getCurrentweather错误后,将输出addressService.getLatitudeLongitude的其余日志。

1 个答案:

答案 0 :(得分:1)

您需要等待函数使用返回的promises完成(并返回promises)。您现在将异步代码视为同步代码

.service('addressService', function($http) {
    var addressService = this;
    addressService.getLatitudeLongitude = function(address) {
        console.log('in addressService.getLatitudeLongitude')

        //RETURN THIS PROMISE
        return $http.get('/location?address=' + encodeURIComponent(address))
        .success(function(data, status, headers, config) {
            console.log('success')
            console.log(data);
            return data;
        })
        .error(function(data, status, headers,config) {
            console.log('error')
        });
    };
})

.service('weatherService', function($http, addressService){
    var weatherService = this;

    weatherService.getWeather = function(latitude, longitude) {
        console.log('get weather')
        $http.get('/all_weather?latitude=' + latitude + '&longitude=' + longitude)
        .success(function(data, status, headers, config) {
        return data;
        })
        .error(function(data, status, headers, config) {
            return null;
        });
    };

    weatherService.getCurrentWeather = function(address) {
        var lat_lon;

        //WAIT FOR THAT PROMISE TO RESOLVE
        return addressService.getLatitudeLongitude(address).then(function(result){
            lat_lon = result;
            return weatherService.getWeather(lat_lon.lat, lat_lon.lng).currently;
            console.log(lat_lon)
        });

    };
})