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

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

最佳答案

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

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

});

关于javascript - Node.js/Javascript - 等到方法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009517/

相关文章:

javascript - 提取包含换行符之间匹配的文本

node.js - nodejs - 如何检查代码的哪一部分通过单元测试进行了测试?

c++ - 如何在 boolean 值(假和真)上编写 `for` 循环

javascript - Bootstrap 轮播页面指示器错误对齐

javascript - 如何使用 Javascript 将当前时间与格式化为字符串的时间范围进行比较

node.js - 如何使用 Bookshelf/Knex 在 Postgres 中插入/更新 `ARRAY` 列

c++ - 无法填充 C++ 数组,每个索引处只有最后一项

javascript - 根据key和values将对象修改为单独的对象

javascript - WebStorm:直接用箭头提取函数

java - 如何将某个值与数组中的值进行比较?