Answer the question
In order to leave comments, you need to log in
Is the foreach loop asynchronous in nodejs?
Getting the price of items on Steam. There is only one subject. The code is something like this
console.log("Call result: " + checkPrice(items));
function checkPrice(items) {
var price = 0;
items.forEach(function(item, index, array) {
market.getItemsPrice(730, item.market_hash_name, function(data) {
price = price + parseFloat(data[item.market_hash_name].lowest_price.replace("$",""));
console.log("Function result: " + price);
});
});
return price;
}
Call result: 0
Function result: 0.06
[{Appid: 730,
classid: '310 776 773',
instanceid: '302 028 390',
currency: false,
background_color: '',
icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHL
bXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot6-iFBRw7P7NYjV96tOkkZOfqPH9Ib7um25V4dB8xLuVo
IqkiVHtrkFoMGjzdoSWdwY6aFvX_Vfow-_mhJS5vs_IwHRhuyk8pSGK2gnD2Gc',
icon_url_large: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4o
FJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot6-iFBRw7P7NYjV96tOkkZOfqPH9Ib7um25V4dB
8teXA54vwxgbg_hVtMjz6cYSRIQc- M1rT_1G6yO3ogpDqvpzAyHBmuHIj7HnfnhypwUYb6FJ3cHs',
descriptions: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
,
tradable: true,
actions: [ [Object] ],
name: 'AUG | Condemned',
name_color: 'D2D2D2',
type: 'Industrial Grade Rifle',
market_name: 'AUG | Condemned (Field-Tested)',
market_hash_name: 'AUG | Condemned (Field-Tested)',
market_actions: [ [Object] ],
commodity: false,
market_tradable_restriction: 7 } ]
Answer the question
In order to leave comments, you need to log in
Promises to help:
checkPrice(items).then(console.log.bind(console, "Call result: "));
function checkPrice(items) {
return Promise.all(items.map(function(item, index, array) {
return new Promise(function(resolve) {
market.getItemsPrice(730, item.market_hash_name, function(data) {
var price = parseFloat(data[item.market_hash_name].lowest_price.replace("$",""));
resolve(price);
console.log("Function result: " + price);
});
});
})).then(function(prices) {
//сумма всех полученных цен
return prices.reduce(function(result, price) {
result += price;
return result;
}, 0);
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question