Javascript 尝试将承诺转换为生成器
我正试图将我的承诺函数转换为生成函数,但为了实现这一点,我做了一些努力 我一直在控制台日志中看到:Javascript 尝试将承诺转换为生成器,javascript,Javascript,我正试图将我的承诺函数转换为生成函数,但为了实现这一点,我做了一些努力 我一直在控制台日志中看到: var myGen {[[GeneratorStatus]]: "suspended"} 当我把它写成承诺时,它看起来是这样的: function maxYvalue2() { return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(re
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'
}
}
};
函数maxYvalue2(){
返回Rpt_scn_cost_v.find({filter:{where:{scenario_id:$stateparms.id}}})。$promise.then(函数(响应){
var maxYvalue=0;
var currMaxYvalue=0;
对于(变量i=0;i最大值){
maxYvalue=当前maxYvalue;
};
}
返回最大值;
});
};
maxYvalue 2()。然后(函数(maxYvalue){
var mxY=最大值
console.log('var',mxY)
$scope.options\u scn\u cst={
图表:{
键入:“折线图”,
身高:450,
保证金:{
前20名,
右:20,,
底数:40,
左:55
},
函数(d){return d.x;},
y:函数(d){返回d.y;},
useInteractiveGuideline:对,
派遣:{
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){返回d3.time.format(“%b%y”)(新日期(d));}
},
yDomain:[0,mxY],//生成器不是承诺的替代品
如果您想简化语法并避免调用,那么请使用异步/等待(可能使用trans-piler)。当然,执行仍然是异步的,无法更改。您可以编写
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;
}
异步函数maxYvalue2(){
var response=wait Rpt_scn_cost_v.find({filter:{where:{scenario_id:$stateParams.id}})。$promise;
// ^^^^^
var maxYvalue=0;
var currMaxYvalue=0;
对于(变量i=0;i最大值){
maxYvalue=当前maxYvalue;
};
}
返回最大值;
}
它与您当前拥有的函数maxYvalue2完全相同,并且它仍然返回一个承诺,因此您可以用完全相同的方式调用它。您试图做的事情毫无意义。生成器与承诺完全不同。