gpt4 book ai didi

javascript - 如何在异步函数之外设置一个 var 以在 $scope 对象中使用

转载 作者:行者123 更新时间:2023-11-28 18:16:56 25 4
gpt4 key购买 nike

在过去的两天里,我一直在阅读有关 Promise、回调和生成器的内容,试图弄清楚如何从异步函数中设置一个值,然后在 $scope 对象中使用该值。

我无法让 promise 解析为 var,它只返回 promise 而不是值,根据我的理解,这是它应该做的事情。这就是我认为发电机可能会有所帮助的地方?

那么,如何从异步函数设置 var 以便我可以在对象 $scope.options_scn_cst 中使用该 var

这是专门针对 nvd3 的,因为我想要做的就是循环遍历回送函数 Rpt_scn_cost_v 的结果以找到最大值,然后将其设置为最大值nvd3 的 yDomain: [min, max] 中的 Y 轴。

我的代码现在看起来像这样:

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;
console.log("yVal: ", maxYvalue)
});
};

var mxY = 100000 // <======== when I set it to this maxYvalue2(), it resolves in a promise

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

然后我像这样调用 $scope 对象:

<nvd3 data="data_scn_cst" options="options_scn_cst"></nvd3>

我也使用 $q 尝试过这种方式,但它返回的是 promise 而不是值......

function maxYvalue2() {

var deferred = $q.defer();

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;
};
}
deferred.resolve(maxYvalue)
console.log("yVal: ", maxYvalue)
});

return deferred.promise
};

var mxY = maxYvalue2() // <======== when I set it to this maxYvalue2(), it resolves in a promise

console.log('var', mxY)

最佳答案

我认为

Rpt_scn_cost_v.find 返回一个 promise 。这使您可以链接 promise 并使用结果:

function maxYvalue2() {
return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).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) {
console.log('maxYvalue=', maxYvalue);
});

关于javascript - 如何在异步函数之外设置一个 var 以在 $scope 对象中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40665311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com