B
B
bqio2016-12-21 13:31:35
Node.js
bqio, 2016-12-21 13:31:35

How can I make the script wait for the function to complete?

var game = {
    users: [{}]
}

game.users[0].steamid = '76561198166206753';
game.users[1].steamid = '76561198166206756';

InventoryParse(game.users[0].steamid, '730');
console.log('Posle: ' + game.users[0].skins); //undefined

Why undefined?
The function itself:
function InventoryParse(SteamId, GameId)
{
    var rq = require('request');
    rq('http://steamcommunity.com/inventory/'+SteamId+'/'+GameId+'/2?l=english', function (error, response, body) {
        if (!error && response.statusCode == 200)
        {
            var json = JSON.parse(body);
            for (var i = 0; i < game.users.length; i++) {
                if (game.users[i].steamid == SteamId) {
                    game.users[i].skins = [];
                    for (var j = 0; j < json.assets.length; j++) {
                        game.users[i].skins.push(json.assets[j].classid);
                    }
                }
            }
            console.log('Do: ' + game.users[0].skins); //Главное здесь выводится, а там нет
        }
    });
}

88ce1f9792594a53abb11e85146a52f0.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2016-12-21
@Arik

Make 3 arguments to the function, and call it at the right place. Then the call will be like

InventoryParse(game.users[0].steamid, '730', function () {
console.log('Posle: ' + game.users[0].skins);
});

And you can read about promises

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question