だから私は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
、modules配列から変数を取得する唯一の方法は、と呼ばれるこのコールバック関数を使用すること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
});
どういうわけか?