2016-11-18 6 views
1

Ich versuche, meine Versprechen-Funktion in eine Generator-Funktion zu konvertieren, aber ich habe ein wenig Mühe, dies zu erreichen.versucht, ein Versprechen in einen Generator zu konvertieren

Ich halte dies in meinem Konsolenprotokoll bekommen:

var myGen {[[GeneratorStatus]]: "suspended"} 

, wenn ich es als ein Versprechen schrieb, es sah wie folgt aus:

function maxYvalue2() { 

    return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){ 
     var maxYvalue = 0; 
     var currMaxYvalue = 0; 
     for (var i=0;i<response.length;i++) { 
      var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost); 
      if (currMaxYvalue > maxYvalue) { 
       maxYvalue = currMaxYvalue; 
      }; 
     } 
     return maxYvalue; 
    }); 

}; 


maxYvalue2().then(function(maxYvalue) { 


    var mxY = maxYvalue 

    console.log('var', 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, mxY], // <============ I then set mxY here in the $scope object 
       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' 
       } 
      } 
     }; 

    //console.log($scope.data_scn_cst); 
}); 

und dann habe ich versucht, als eine neu zu schreiben Generator wie folgt:

var myGen = function*() { 

    var maxYvalue = 0; 
    var currMaxYvalue = 0; 

    var response = yield Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}); 

    for (var i=0;i<response.length;i++) { 
     var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost); 
     if (currMaxYvalue > maxYvalue) { 
      maxYvalue = currMaxYvalue; 
     }; 
    } 

}; 

var mxY = myGen(); 

console.log(mxY.next(1)); 



console.log('var', 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, mxY], // <============ I then set mxY here in the $scope object 
      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' 
      } 
     } 
    }; 
+0

Was Sie versuchen, macht keinen Sinn. Ein Generator ist etwas ganz anderes als ein Versprechen. – Bergi

Antwort

1

Generatoren sind kein Ersatz für Versprechungen.

Wenn Sie Ihre Syntax vereinfachen und then Anrufe zu vermeiden, verwenden async/await (möglicherweise mit einem trans ­ piler). Die Ausführung wird natürlich immer noch asynchron sein, es gibt keine Möglichkeit das zu ändern. Sie würden

async function maxYvalue2() { 
    var response = await Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise; 
//     ^^^^^ 
    var maxYvalue = 0; 
    var currMaxYvalue = 0; 
    for (var i=0;i<response.length;i++) { 
     var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost); 
     if (currMaxYvalue > maxYvalue) { 
      maxYvalue = currMaxYvalue; 
     }; 
    } 
    return maxYvalue; 
} 

schreiben, die genau auf die function maxYvalue2 entspricht, die Sie derzeit haben, und es gibt noch ein Versprechen, so dass Sie würde es auf genau die gleiche Art und Weise nennen.

Verwandte Themen