How do you run a function that pull data in a loop and have the data by the end of the loop?

Here is what I am trying to do. My first function loops through an array of user ids and builds an array of usernames for displaying in the console. This is using node.js.

function Function1(){
    for (var i = 0; i < array.length; i++){
        if (array[i] !== '') {
                GetUserName(votelog[i]);
        }
    }
    console.log(array2);
    }
}

function GetUserName(userid){
    client.query("SELECT `username` FROM " + config.database.tablenames.user + " WHERE `userid` = '" + userid + "'",
                function selectCb(err, results, fields) {
                    if (err) { throw err; }
                    console.log(results);
                    console.log(fields);
                    client.end();
                    array2.push(results.username);
                });
}

However array2 is blank. I have seen that JavaScript doesn't stop like C# does when calling a function. How would I rewrite this so this works as I expect?

There are a couple answers to this, and probably the "correct" way would be to not fetch each username individually like that but to write the query to return all the usernames at once. If you want to keep individual username queries, you can do something like:

function Function1(){
    for (var i = 0; i < array.length; i++){
        if (array[i] !== '') {
            GetUserName(array[i], function(result) {
                array2.push(result);
                //  Verify that all of the results have come back
                if(array2.length == array.length) {
                    console.log(array2);
                }
            });
        }
    }
}

function GetUserName(userid, callback){
    client.query("SELECT `username` FROM " + config.database.tablenames.user + " WHERE     `userid` = '" + userid + "'",
    function selectCb(err, results, fields) {
        if (err) { throw err; }
        console.log(results);
        console.log(fields);
        client.end();
        callback(results.username);
    });
}

What I added was a conditional on the callback to console.log the results only if the number of returned usernames equals the number of userids in the original array. That indicates that all of the queries have completed.

You will also have to handle the case where there is an error in GetUserName, because that could cause console.log() to never be called assuming you're planning on handling the error and not just letting it crash your application.

Disclaimer; I'm not very used to JavaScript (or Node) myself. Anyway, my take is something like this might work:

function Function1(){
    for (var i = 0; i < array.length; i++){
        if (array[i] !== '') {
            GetUserName(votelog[i], function(result) {
                array2.push(result);
            });
        }
    }
    console.log(array2);
}

function GetUserName(userid, callback){
    client.query("SELECT `username` FROM " + config.database.tablenames.user + " WHERE     `userid` = '" + userid + "'",
                function selectCb(err, results, fields) {
                    if (err) { throw err; }
                    console.log(results);
                    console.log(fields);
                    client.end();
                    callback(results.username);
                });
}