title: setting a promise to an external variable
body: <p>I have a function that returns a promise (I believe, I'm new to javascript) which I'm trying to set to a variable <code>mxY</code>. I've written my functions like this:</p>
  
  <pre><code>function maxYvalue2() {
      return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
          var maxYvalue = 0
          for (var i=0;i&lt;response.length;i++) {
              var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
              if (currMaxYvalue &gt; maxYvalue) {
                  maxYvalue = currMaxYvalue
              };
          }
          console.log("yVal: " + maxYvalue)
          return maxYvalue;
      });
  };
  
  var mxY = maxYvalue2().then(function (response) {
      console.log("fnc: ", response);
      return response;
  });
  
  
  console.log(mxY);
  </code></pre>
  
  <p>and my console shows this, where <code>mxY</code> seems to log a <code>{$$state: Object}</code>:</p>
  
  <pre><code>reports.controller.js:99 d {$$state: Object}
      $$state: Object
          status: 1
          value: 78820.3574413
          __proto__: Object
        __proto__: Object
  reports.controller.js:88 yVal: 78820.3574413
  reports.controller.js:94 fnc:  78820.3574413
  </code></pre>
  
  <p>from what I understand so far, the <code>.then</code> in <code>maxYvalue2()</code> is returning a promise, which is why I get back <code>$$state.Object</code>, but what I need to do is "unwrap" the promise when it resolves, is that right or am I completely off here? And then I set it to the variable?</p>
  
  <p>------EDIT---------</p>
  
  <p>I'm trying to get the result of <code>maxYvalue2()</code> to go here...change <code>yDomain: [0, 100000]</code> to <code>yDomain: [0, mxY]</code> in the highlighted area below:</p>
  
  <pre><code>function maxYvalue2() {
      return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
          var maxYvalue = 0
          for (var i=0;i&lt;response.length;i++) {
              var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
              if (currMaxYvalue &gt; maxYvalue) {
                  maxYvalue = currMaxYvalue
              };
          }
          console.log("yVal: " + maxYvalue)
          return maxYvalue;
      });
  };
  
  var mxY = maxYvalue2().then(function (response) {
      console.log("fnc: ", response);
      return response;
  });
  
  
  console.log(mxY);
  
  
      $scope.options_scn_cst = {
              chart: {
                  type: 'lineChart',
                  height: 450,
                  margin : {
                      top: 20,
                      right: 20,
                      bottom: 40,
                      left: 55
                  },
                  x: function(d){ return d.x; },
                  y: function(d){ return d.y; },
                  useInteractiveGuideline: true,
                  dispatch: {
                      stateChange: function(e){ console.log("stateChange"); },
                      changeState: function(e){ console.log("changeState"); },
                      tooltipShow: function(e){ console.log("tooltipShow"); },
                      tooltipHide: function(e){ console.log("tooltipHide"); }
                  },
                  xAxis: {
                      axisLabel: '',
                      tickFormat: function(d) { return d3.time.format('%b %y')(new Date(d)); }
                  },
                  yDomain: [0, 100000], //&lt;======change the 100000 to the var mxY
                  yAxis: {
                      axisLabel: '$ / month',
                      tickFormat: function(d){
                          return d3.format('$,.0f')(d);
                      },
                      axisLabelDistance: -10
                  },
                  callback: function(chart){}
              },
              title: {
                  enable: true,
                  text: 'Scenario Costs Over Time'
              },
              subtitle: {
                  enable: false,
                  text: 'Put your Subtitle here.',
                  css: {
                      'text-align': 'center',
                      'margin': '10px 13px 0px 7px'
                  }
              },
              caption: {
                  enable: false,
                  html: 'Put your Caption Here.',
                  css: {
                      'text-align': 'justify',
                      'margin': '10px 13px 0px 7px'
                  }
              }
          };
  </code></pre>