2016-11-18 34 views
1

我试图将我的承诺函数转换为生成器函数,但我正在努力实现这一点。试图将承诺转换为生成器

我不断收到这在我的控制台日志:

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

当我写它当作是一种承诺,它是这样的:

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); 
}); 

,然后我试图重新写为发电机是这样的:

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

你试图做的没有意义。发电机与承诺完全不同。 – Bergi

回答

1

发电机不是承诺的替代品。

如果您希望简化的语法和避免then通话,使用async/await(可能有反式­堆垛机)。当然执行仍然是异步的,没有办法改变它。你会写

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; 
} 

这是完全等同于你目前拥有的function maxYvalue2,它仍然会返回一个承诺,所以你把它在完全相同的方式。