title: Convert a function to a angular promise so $scope.* is not undefined
body: <p>Tried to minify my code so it is clear for you to see what i want to accomplish but not succeed. </p>
  
  <p>What i want to accomplish is that when i do <code>console.log($scope.tempData);</code> i see the data instead of undefined so i can use it in the controller. I allready know that promises are the way to go, because of my question asked here on stackoverflow: <a href="https://stackoverflow.com/questions/34660284/scope-variable-is-undefined-when-it-is-set-inside-a-function/34666219#34666219">$Scope variable is undefined</a></p>
  
  <p>But what i don't know is how i can get a promise from <code>$scope.getCube();</code> so i can just do <code>$scope.getCube().then(function (data) { ... });</code> i found this: <a href="https://stackoverflow.com/questions/9516900/how-can-i-create-an-asynchronous-function-in-javascript">how to create asynchronous function</a> but the answer was not clear enough for me.  </p>
  
  <p>/////////// Side Question ////////////</p>
  
  <p><em>The code i provide on the bottom is what i have found on Github, not everything of that code is clear for me:</em> Why is ClientService.dynamic() a promise? There is no <code>q.defer</code> in <code>this.dynamic = function (params)</code>?</p>
  
  <p>//////////////////////////////////////////////</p>
  
  <p>for a better reading of the code: <strong><a href="https://jsfiddle.net/vz4d06br/" rel="nofollow noreferrer">JSFiddle</a></strong></p>
  
  <pre><code>// CONTROLLER
  
  $scope.buildObject = function () {
  
          $scope.getCube(); // 
  
          console.log($scope.tempData); //gives undefined, obvious because getCube is not done running yet.
  
      }
  
      $scope.getCube = function () {
  
          promise = ClientService.dynamic(params);
  
          promise.then(function (data) {
  
              $scope.tempData = data[0];
  
              return $scope.tempData;
  
          }
  
      }
  
  /////////////////////////////////////////////////////
  
  // DYNAMIC SERVICE
  
  this.dynamic = function (params) {
  
      return qvCommService.send(createHyperCube).then(function (data) {
  
      }
  
  }
  
  //////////////////////////////////////////////////
  
  // SEND SERVICE
  
  send: function (msg) { 
  
      var deferred = $q.defer();
      var promise = deferred.promise;
  
      this.socket = new WebSocket(ws://*************);
  
      this.socket.addEventListener('open', function (e) {
          deferred.resolve("connected")
  
      });
  
      this.socket.send(angular.toJson(msg))
  
      return promise;
  }
  </code></pre>