Node.js / Javascript - 等到方法完成

时间:2015-11-30 22:53:02

标签: javascript node.js for-loop methods wait

我正在编写一个Steam交易机器人,但我遇到的问题是for循环不会等到for循环内的方法完成。所以代码不能正常工作。

for (i = 0; i < offer.itemsToReceive.length; i++) {

    console.log(offer.itemsToReceive[i].market_hash_name);

    community.getMarketItem(appid.CSGO, offer.itemsToReceive[i].market_hash_name, function(err, items) {
        if (err) {
            Winston.error("Error getting Marketprice");
        } else {
            var cacheItemPrice = items.lowestPrice;
            totalValue += items.lowestPrice;
            Winston.info("Item " + offer.itemsToReceive[i].market_hash_name + " is " + items.lowestPrice + " Cents worth");
            if (items.lowestPrice <= minValue) {
                minValue = items.lowestPrice;
            }
        }

    });
}

如果循环没有等到方法结束,变量i在方法中不正确,我得到错误的结果。


修改
现在,当我将@Cleiton中的代码放入我希望返回两个值的函数中时,函数会在方法有时间更改之前返回该值。

function checkItemPricesCashIn(offer) {
    Winston.info("Getting itemprices from trade #" + offer.id);
    var totalValue = 0;
    var minValue = 50;

    var executionList = [];

    function getMarketItem(item) {
        var market_hash_name = item.market_hash_name;
        return function() {
            community.getMarketItem(appid.CSGO, market_hash_name, function(err, items) {
                if (err) {
                    Winston.error("Error getting Marketprice");
                } else {
                    var cacheItemPrice = items.lowestPrice;
                    totalValue += items.lowestPrice;
                    Winston.info("Item " + market_hash_name + " is " + items.lowestPrice + " Cents worth");
                    if (items.lowestPrice <= minValue) {
                        minValue = items.lowestPrice;
                    }
                } 
                (executionList.shift() || function() {})();
            });
        }
    }

    offer.itemsToReceive.forEach(function(item) {
        executionList.push(getMarketItem(item));
    });

    if (executionList.length) {
        executionList.shift()();
    }

    console.log(totalValue);
    console.log(minValue);

    return {
        totalValue: totalValue,
        minValue: minValue
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将某些库用作异步https://github.com/caolan/async。并且它与每个系列或每个系统都有任何异步循环。

async.each(array, function(item, callback){
    // do something with a item
    // call callback when finished
    callback();
});

答案 1 :(得分:1)

下面是一个完整的工作代码,我希望这会有所帮助:)

function checkItemPricesCashIn(offer, cb) {
Winston.info("Getting itemprices from trade #" + offer.id);
var totalValue = 0;
var minValue = 50;

var executionList = [];

function getMarketItem(item) {
    var market_hash_name = item.market_hash_name;
    return function() {
        community.getMarketItem(appid.CSGO, market_hash_name, function(err, items) {
            if (err) {
                Winston.error("Error getting Marketprice");
            } else {
                var cacheItemPrice = items.lowestPrice;
                totalValue += items.lowestPrice;
                Winston.info("Item " + market_hash_name + " is " + items.lowestPrice + " Cents worth");
                if (items.lowestPrice <= minValue) {
                    minValue = items.lowestPrice;
                }
            } 
            (executionList.shift() || cb)(minValue,totalValue);
        });
    }
}

offer.itemsToReceive.forEach(function(item) {
    executionList.push(getMarketItem(item));
});

if (executionList.length) {
    executionList.shift()();
}
}

使用方法:

checkItemPricesCashIn(yourOffer/*your offer*/, function(min, max){
//it will be execute just after
console.log('min', min);
console.log('max', max);

});