2017-03-27 4 views
1

私は2つの要求をしているが、私は結果から値を得る場合は、私は2つの異なる約束のrequrest結果に依存するので、それぞれの約束の結果に基づいて機能を実行する私はどのように私はそれを解決することができないのか分からない。 2 promisses一緒にnullを返す

私のコードコントローラー:

$scope.originLatLong = null; 
    $scope.destinationLatLong = null; 

    //Get LAT and LONG from origin and destionation http://something/{Code} 
    $http.get('something/getLatLng/'+$scope.originAirport).then(function(response){ 
     $scope.originLatLong = response.data; //doesnt return null 

    }); 

$http.get('something/'+$scope.destinationAirport).then(function(response){ 
     $scope.destinationLatLong = response.data; //doesnt return null 

    }); 

console.log($scope.originLatLong) //returns null 
console.log($scope.destinationLatLong) //returns null 
var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong); 

答えて

1

このようにしてみてください。

$scope.originLatLong = null; 
$scope.destinationLatLong = null; 

$http.get('something/getLatLng/'+$scope.originAirport).then(function(response){ 
    $scope.originLatLong = response.data; 
    return $http.get('something/'+$scope.destinationAirport) 
}) 
.then(function(response) { 
    $scope.destinationLatLong = response.data; 
    var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong); 
}) 

をあなたは.then()外distanceTotalが必要な場合や、HTTP呼び出しの前にそれを宣言:

$scope.originLatLong = null; 
$scope.destinationLatLong = null; 
var distanceTotal; 

$http.get('something/getLatLng/'+$scope.originAirport).then(function(response){ 
    $scope.originLatLong = response.data; 
    return $http.get('something/'+$scope.destinationAirport) 
}) 
.then(function(response) { 
    $scope.destinationLatLong = response.data; 
    distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong); 
}) 

元の問題の説明で編集:

$httpの呼び出しは非同期です。つまり、ブラウザが要求を行い、その後のコードは、ブラウザがサーバーからの応答を待っている間に実行を継続します。これは、コードがあなたの例で実行された順序が、それはなぜconsole.logsを簡単に確認することができ

$http call 
The other $http call 
console.log($scope.originLatLong) 
console.log($scope.destinationLatLong) 
var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong); 
$scope.originLatLong = response.data; 
$scope.destinationLatLong = response.data; 

のようなものにconsole.log()sで、未定義/変数がまだnullでどのように見ていたことを意味しますは未定義です。混乱へ

別の編集:

あなたは.then()機能外で定義されるようにdistanceTotalを負いかねます。それが定義される唯一の保証された場所は、then()です。

+0

ありがとうございました。私が間違っていたこと、またはなぜ失敗したのか教えていただけますか? – Pedro

+0

こんにちは、私はpromiseの外でconsole.log(distanceTotal)を作って何も表示せず、私に未定義を与えました。 " – Pedro

+0

私の編集をチェックしてください;問題は同じでしょう。' distanceTotal'を使用して、 .then() '。 – Fissio

0

これは複数の約束があり、両方の応答を併用したいので、私はこれを$q.allで解決します。

私たちがする必要があるのは、約束事の配列を作成することだけです。そして、$q.allとすれば、両方の約束の応答を1つの.then()で得ることができます。方法は次のとおりです。

var promises = []; 
promises.push($http.get('something/getLatLng/'+$scope.originAirport)); 
promises.push($http.get('something/'+$scope.destinationAirport)); 

$q.all(promises).then(function(response) { 
    $scope.originLatLong = response[0].data; 
    $scope.destinationLatLong = response[1].data; 

    console.log($scope.originLatLong) 
    console.log($scope.destinationLatLong) 
    var distanceTotal = calculate($scope.destinationLatLong, $scope.originLatLong); 
    ... 
}); 
関連する問題