title: Why is my code still running as asynchronous even though I have the Promise and await values
body: <p>I need to run a <code>if else</code> statement then run a map inside of this if else, and after that run the queries, but it's just running as asynchronous. </p>
  
  <p>This is the code below that is broken. I have no idea how to fix it.</p>
  
  <pre><code>function newRank() {
      return new Promise((resolve, reject) =&gt; {
          // If the select from target table is empty, then run this if below
          if (rank_atual.length == 0) {
              console.log('RANK ATUAL IF')
              // Map new rank to add into target table
              rank_gender.map(async (rnk) =&gt; {
                  // Query that will be feed by rank array and put that on the target table
                  let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, current_timestamp(), NULL, 1, ${rnk.genero}, 1, 1)`;
                  // console.log(sql_insert)
                  // Run the query above, and check if everything goes fine.
                  conInsert.query(sql_insert, function (err, result) {
                      if (err) return reject(err);
                  })
              })
          }
  else {
      console.log('RANK ATUAL ELSE')
      // Map new rank to add into target table
      rank_gender.map((rnk) =&gt; {
          // Find the index on the actual rank using the new rank fonogram
          let ra = rank_atual.findIndex(obj =&gt; obj.fk_id_fonogramas == rnk.fonograma)
          // if the position have been find, run the condition below
          if (ra &gt; 0) {
              console.log(rnk.fonograma)
              console.log(rank_atual[ra].fk_id_fonogramas)
              console.log(rank_atual[ra].nr_rank_sem_genero)
              // if the actual rank is less then NEW rank, then run the condition below
              if (rnk.rank &lt; rank_atual[ra].nr_rank_sem_genero &amp;&amp; rnk.rank_genero &lt; rank_atual[ra].nr_rank_com_genero ) {
                  // console.log(rank_atual[ra + 1].nr_rank_sem_genero)
                  // Query to update the old record changing the date and the flag to set inactive
                  let sql_update = `UPDATE rnk_rank_diario SET dh_sem_genero_alterado = current_timestamp(), dh_com_genero_alterado = current_timestamp(), fl_ativo = 0 WHERE fk_id_fonogramas = ${rank_atual[ra].nr_rank_sem_genero} AND fk_id_rnk_local_record = 1 AND fk_id_rnk_tipo_emissora = 5 AND fl_ativo = 1;`;
  
                  // Query to insert the new record with the new values.
                  let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, '${moment(rank_atual[ra].dh_sem_genero_criado).utc().format('YYYY-MM-DD HH:MM:SS')}', NULL, 1, ${rnk.genero}, 1, 1);`;
  
  
                  // Run the update query, and check if everything goes fine.
                  conInsert.query(sql_insert, function (err, result) {
                      if (err) return reject(err)
                  });
  
                  // Run the insert query, and check if everything goes fine.
                  conInsert.query(sql_update, function (err, result) {
                      if (err) return reject(err)
                  });
              }
          } else {
              console.log('OTHER ELSE')
              // Query to create new records on the table if that song doesn't exist yet
              let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, current_timestamp(), NULL, 1, ${rnk.genero}, 1, 1);`;
              // console.log(sql_insert)
  
              // Run the query above, and check if everything goes fine.
              conInsert.query(sql_insert, function (err, result) {
                  if (err) return reject(err);
              })
          }
      })
  }
  </code></pre>
  
  <p>called by: </p>
  
  <pre><code>// Rodar as funções uma a uma.
  async function mestre() {
      // Function to get the new rank from rnkYYYYMM table
      let result = await runSelectRank();
      // Function to get the actual rank from rnk_rank_diario
      result = await runSelectRank_Atual();
      // Function to get the rank and insert them on the rank key inside of the objects
      result = await getResult();
      // Function to get the rank based on music gender
      result = await getGenderResult();
      // Function to verify and create the new ranks
      result = await newRank();
      // Function to close those connectios that were opened to insert into tables
      result = await conInsert.end();
      // Function to close those connectios that were opened to select from tables
      result = await conSelect.end();
  }
  </code></pre>
  
  <p>this is an example of what the code is doing instead of waiting finishing and running</p>
  
  <pre><code>30354
  30354
  776
  18758
  18758
  946
  43614
  43614
  498
  28963
  28963
  839
  9814
  9814
  834
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  OTHER ELSE
  5. newRank rodou ok
  </code></pre>
  
  <p>When <code>5. newRank rodou ok</code> shows on the screen, then the queries start to run, after pass by ALL <code>if else</code> statement, am I missing something?? </p>