0

Why does the push to inventory not work? When I log inventory, all that prints in [].

var request = require('request'),
stockJSON = 'https://www.supremenewyork.com/mobile_stock.json?c=1';
var inventory = [];
request({
    headers: {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
},
uri: stockJSON
}, function(err, resp, body) {
    var json = JSON.parse(body);
    inventory.push(json.products_and_categories.new);
});

console.log(inventory);
  • It does work, just not when you apparently expect it to... – jonrsharpe Dec 19 '16 at 21:06
  • Please read up on promises. That's what you need to solve your problem. The inventory is empty if you log it because it does not wait for the function to be executed. If you would set `setTimeout(function(){console.log(inventory)}, 5000);` instead of your log function, it would work. – Randy Dec 19 '16 at 21:13

0 Answers0