MySQL, Node.js Sequential actions - How can I do that?
<p>I've the following code:</p>

<pre><code>function query1() {

var defered = Q.defer();

console.log("In query1");

var connection = mysql.createConnection({
    host: '........',
    user: 'm...c....a.....i',
    password: '......Z....9...K',
    database: '.....ol'
});

connection.connect(function(err) {
    if (!err) {
        console.log("Database is connected ...");
    } else {
        console.log("Error connecting database ...");
    }
});

sql = '' +
    'select c.ID as CENA_ID, ' +
    '      c.I_KEY as CENA_NUMERO, ' +
    '      c.NM_CENA as CENA_NOME, ' +
    '      b.DS_MAC as MAC_BOX, ' +
    '      v.DS_CLIENTID as ALEXA_ID, ' +
    '    v.FK_ID_GRUPO as GRUPO_ID ' +
    '  from TB_DISPOSITIVOS_VOZ v ' +
    '      inner join TB_GRUPOS g ' +
    '      on g.ID = v.FK_ID_GRUPO ' +
    '      inner join TB_CENAS c ' +
    '      on g.ID = c.FK_ID_GRUPO ' +
    '      inner join TB_CENTRAIS b ' +
    '      on g.ID = b.FK_ID_GRUPO ' +
    'where v.DS_CLIENTID = "' + userId + '" ' +
    'and lower(c.NM_CENA) like "%' + sceneName.toLowerCase() + '%"';


console.log("Created query");

try{

    connection.query(sql, function(erro, rows, fields) {

        if (!erro) {

            console.log("Executed query verifying the userId");

            contador = 0;

            if (rows.length &gt; 0) {

                cena_id = rows[0].CENA_ID;
                cena_numero = rows[0].CENA_NUMERO;
                cena_nome = rows[0].CENA_NOME;
                alexa_id = rows[0].ALEXA_ID;
                grupo_id = rows[0].GRUPO_ID;
                mac_box = rows[0].MAC_BOX;

                contador = contador + 1;

            }

            console.log("contador: " + contador);

        } else {
            console.log("Error - getting the Alexa register in database" + erro);
            context.fail("Error - getting the Alexa register in database" + erro);
        }

    });


}catch (ex){
    console.log("exception: " + ex);
}
</code></pre>

<p>}</p>

<p>And this code as well:</p>

<pre><code>Q.all([query1()]).then(function(results) {

        console.log("Q.all log function");

        if (contador &gt; 0) {

            console.log("contador &gt; 0");

            var client = mqtt.connect('mqtt://.............com');
            console.log("connected to MQTT broker");

            var buffer = [26,
                0,0,0,0,555,645,0,0,0,0,0,
                0,5555,2,Math.floor((Math.random() * 200) + 1),
                0,0,0,333,13,4,0,1,0,
                cena_numero
            ];

            console.log("Created buffer");

            client.on('connect', function() {

                client.publish('n/c/' + mac_box + '/app', buffer);

                console.log("sent MQTT");

            });

            speechOutput = "Command " + sceneName + " executed successfully";
            repromptText = "";
            console.log("Process executed successfully")

        } else {

            console.log("contador &lt;= 0");

            speechOutput = "This command was not found!";
            repromptText = "";

        }

    }, function (reason) {

        console.log("reason: " + reason);

    });
</code></pre>

<p>How can I do for the second code execute only if the first <code>query1()</code> executed correctly? Because in the function <code>query1()</code> i've a MySQL Query, and I only can continue with the process after the result of this query.</p>

<p>Anyone can help me?</p>

<p>Thanks a lot!</p>