所以我必须share
循环计算一些。在该循环的每次迭代中,我都必须rent
从数组中调用一个变量。所以我calculate
从数据库的东西中分离了功能。
var calculate = function() {
while(count < 100) {
var share = 50;
var shareArray = [];
for(var i = 0; i < 100; i++) {
var pension = share*2; // mathematical stuff
// Gets a rent from a database and returns it in a callback
getRent(modules, share, function(rent) {
share = rent*foo; // some fancy mathematical stuff going on here
// I need to get the share variable above out of its function scope
});
// I need the share variable right here
shareArray.push(share); // the value of share will be for i = 0: 50, i= 1: 50 ...
// This is not what i want, i need the share value from getRent()
}
count++;
}
}
现在您可能会看到,我遇到了以下麻烦。因为我在 node.js 中工作,所以rent
从模块数组中获取变量的唯一方法是通过这个名为getRent()
. 问题是,我需要share
这一步之后的值,但在getRent()
. 有什么办法我可以做到这一点?
这是getRent()
- 功能:
var getRent = function(modules, share, callback) {
// Searching for a fitting rent in the modules array
// Just assume this is happening here
callback(rent);
};
所以问题是:我怎样才能“返回” share
:
getRent(modules, share, function(rent) {
share = rent*foo; // some fancy mathematical stuff going on here
// I need to get the share variable above out of its function scope
});
以任何方式?