title: Assigning a value of a promise to an object method
body: <p>Consider this object i'm creating. </p>
  
  <pre><code>$scope.formules = {};
  $scope.formules = {
  
      label: 'some label',
      getYear: function() {
  
          // getData() is a promise
          $scope.getData().then(function(){
  
              // Some code to get the right data
  
              return someData;
  
          }.catch(function () {
           $scope.error = 'bad error';
  
      }
  
  }();
  }
  </code></pre>
  
  <p>At this point, getYear is undefined, obvious because i have to do a second return. </p>
  
  <pre><code>$scope.formules = {};
  $scope.formules = {
  
      label: 'some label',
      getYear: function() {
  
          // getData() is a promise
          $scope.getData().then(function(){
  
              // Some code to get the right data
  
              return someData;
  
          }.catch(function () {
           $scope.error = 'bad error';
  
      }
  
      return $scope.getData();  //Putting the second return here
  
  }();
  </code></pre>
  
  <p>or a shorter way could be this: </p>
  
  <pre><code>$scope.formules = {
  
      label: 'some label',
      getYear: function() {
  
          // getData() is a promise
          return $scope.getData().then(function(){ // Or putting the second return here.
  
              // Some code to get the right data
  
              return someData;
  
          }.catch(function () {
           $scope.error = 'bad error';
  
      }
  
  
  }();
  </code></pre>
  
  <p>again this is not the right way because the second return just returns the promise so my method <code>getYear</code> contains a promise now, not the right way to do this. </p>
  
  <p><strong>last try:</strong> </p>
  
  <pre><code>$scope.formules = {
  
      label: 'some label',
      getYear: function() {
  
          // getData() is a promise
          return $scope.getData().then(function(){ // return the $scope.getData()
  
              // Some code to get the right data
  
              return someData;
  
          }.catch(function () {
           $scope.error = 'bad error';
  
      }
  
  
  }().then(function(data){ return data; });
  </code></pre>
  
  <p>Now i run out of luck and ideas, <code>console.log($scope.formules)</code> shows me again that getYear is a promise, and won't give me the value. </p>
  
  <p>Note: If i do <code>console.log(data)</code> instead of <code>return data;</code> i do get the value that i would like to bind to the method. </p>
  
  <p>What am i doing wrong here, what is the right way to bind the value? Stack &amp; Google did not gave me any answer...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the actual code that i use inside the method: </p>
  
  <pre><code>getYear: function ()  {
  
      $scope.measure = value.formules_total_year;
  
      $scope.getCube().then(function(test){
  
          var totalRatioData = [];
  
          for (var i = 0; i &lt; $scope.tempData.length; i++){
  
          totalRatioData.push($scope.tempData[i][1].qNum);
  
      }
  
      return totalRatioData;
  
  })
  
  
  }(),
  </code></pre>
  
  <p>$scope.tempData is an array of 5 arrays. <code>[Array[2], Array[2], Array[2], Array[2], Array[2]]</code> and in those 5 arrays you have 2 objects</p>
  
  <pre><code>0: Object
      qElemNumber: 0
      qNum: 2009
      qState: "O"
      qText: "2009"
      __proto__: Object
  1: Object
      qElemNumber: 0
      qNum: 225632.21000000002
      qState: "L"
      qText: "225632,21"
  </code></pre>
  
  <p>What i want as you see in this edit is a new array of all the qNum's of the last object of the 5 arrays and assign this array to getYear.</p>