我有一个返回 promise 的函数(我相信,我是 javascript 新手),我试图将其设置为变量 mxY
。我的函数是这样写的:
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<response.length;i++) {
var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
if (currMaxYvalue > maxYvalue) {
maxYvalue = currMaxYvalue
};
}
console.log("yVal: " + maxYvalue)
return maxYvalue;
});
};
var mxY = maxYvalue2().then(function (response) {
console.log("fnc: ", response);
return response;
});
console.log(mxY);
我的控制台显示了这一点,其中 mxY
似乎记录了 {$$state: Object}
:
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
据我所知,maxYvalue2()
中的 .then
返回一个 promise ,这就是我返回 $$state.Object 的原因
,但我需要做的是在 promise 解决时“解开” promise ,这是正确的还是我完全离开这里了?然后我将它设置为变量?
------编辑---------
我正在尝试将 maxYvalue2()
的结果移至此处...将 yDomain: [0, 100000]
更改为 yDomain: [ 0, mxY]
在下面突出显示的区域:
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<response.length;i++) {
var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
if (currMaxYvalue > 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], //<======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'
}
}
};
最佳答案
一旦有了 promise ,就无法同步解开它。欢迎来到异步编程的土地!
您只能检索 then()
子句(如果存在错误,则为 catch()
子句)内的 Promise 解析。
当您调用 console.log(mxY);
时,您将在创建 Promise 的同一时间点访问 Promise 本身。该 promise 尚未能够兑现。由于 JavaScript 是单线程的,因此您永远无法访问 Promise 的解析,除非异步。
所以,是的,您必须使用 then()
子句。值得庆幸的是,Promise 提供了一种链接 then 的机制。
你可以这样做:
maxYvalue2().then(function (response) {
console.log("fnc: ", response);
return response;
}).then(function (response) {
// do something
return newResponse
}).then(function (newResponse) {
// do something
return notherNewResponse
}).then(function (notherNewResponse) {
// etc
return notherNotherNewResponse
});
这表明您可以链接您的 Promise 并使用它们来执行一些相当复杂的异步计算。
关于javascript - 对外部变量设置 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646379/