title: How do i define a global variable from asynchronous call with angularjs?
body: <p>I want to use single_video variable in outside of the controller function. It prints well in first console log. However it gives a single_video is undefined error in second console.log which is outside of the controller function. Because of the asynchronousity.</p>
  
  <pre><code>   var single_video;
  var app = angular.module('myApp', []);
  app.controller('randomVideo', function($scope, $http) {
      var onSuccess = function(response){
          $scope.video = response.data; 
          single_video = $scope.video;
          //First console.log
          console.log('1st ID='+single_video.yt_id);
      };
      var onError = function(reason){
          $scope.error = "There is an error about getting random_video.php";
      };
     $http.get("http://www.ytmdb.com/ytvideo/api/random_video.php")
     .then(onSuccess, onError);
  });
  //Second console.log
  console.log('2nd ID='+single_video.yt_id);
  </code></pre>