D
D
Dmitry Petrik2016-04-27 19:23:35
JavaScript
Dmitry Petrik, 2016-04-27 19:23:35

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

Result (output order preserved):
Call result: 0
Function result: 0.06

Tried to put return in foreach, then Call result will be undefined. It looks like the checkPrice function doesn't wait for the foreach to complete at all. At what stage did I step?
UPD:
Items are. The content is:
[{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

5 answer(s)
D
Dmitry Belyaev, 2016-04-27
@Flexo

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

R
Roman, 2016-04-27
@r_zaycev

market.getItemsPrice()
asynchronous, judging by the code.

T
timfcsm, 2016-04-27
@timfcsm

market.getItemsPrice asynchronous

A
amf1k, 2016-04-27
@amf1k

And items that is?

I
Ivan, 2016-04-28
@LiguidCool

Hm, why does it seem to me that you are calling the function when items has not yet been received?

function checkPrice(items) {
console.log(items);
//////////
}

It's just a common mistake in such situations.
In general, yes - probably promises.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question