Javascript var scope

Hi I am new to javascript and I have a problem with this code :

connection.query(query, function(err, rows, fields) {
var resultRows = [];            
if (err) throw err;             
if (rows.length === 0) {
    petQuery = "select * from table";
    connection.query(petQuery, function(err, rows, fields) {
        if (err) throw err;
        resultRows = rows;
        console.log(resultRows); // Ouput is correct with data
    });
} else {
    resultRows = rows;
}  

console.log(resultRows); //Ouput is an empty array ([])
...

I am expecting that the second console.log(resultRows) prints the same output as the first one, as the variable is globally declared.

Can anybody tell me what is the problem with this ?

Thanks