所以我必须在循环中计算一些share
。在该循环的每一次迭代中,我都必须从数组中获取一个名为rent
的变量。所以我从数据库中分离出calculate
函数。现在Node.js中的函数范围和回调函数
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);
};
所以,问题是:以任何方式
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
});
:我怎么能 “回归” share
?
“getFromDB()”是什么样的?我不明白这是如何工作的,因为从数据库中提取几乎肯定涉及异步步骤。 – Pointy 2013-02-14 21:58:06
如果你可以做一个**查询来获得你需要的所有值,而不是为每个查询单独查询,你几乎肯定会更好,性能明智。一次只能做一件事会慢很多。 – Pointy 2013-02-14 21:59:22
对不起。这只是一个构建的例子。真正的代码将难以解释这一点。假设这是有效的。嗯,我可以编辑它。 – 2013-02-14 21:59:48