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

Node.js / Javascript - Wait until method is finished

我正在编写一个 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 的代码放入我想要 return 两个值的函数时,函数 return 是方法有时间更改它们之前的值。

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
    }
}

您可以使用一些库作为异步 https://github.com/caolan/async。以及作为每个系列或每个的任何异步循环。

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

下面是完整的工作代码,希望对您有所帮助:)

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);

});