title: Wait for it to resolve promise and then insert neatly
body: <p>I've been watching all morning because this code doesn't work for me. </p>
  
  <p>I want once the promise is resolved, the "then" get its last inserted ID (the promise) and neatly insert into the other table.  Currently first paints all the promise and then just then or disorderly ...</p>
  
  <pre class="lang-js prettyprint-override"><code>con.query("SELECT ID FROM wp_posts ORDER BY ID DESC LIMIT 0,1;", function(err, result, fields) {
    console.log(result);
    for (var i = 0; i &lt; getData.length; i++) {
      var source = getData[i]["source"];
      var text = getData[i]["text"];
      var quality = getData[i]["quality"];
      new Promise(function(resolve, reject) {
        var sql = "INSERT INTO `wp_posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,`post_excerpt`, `post_status`,`comment_status`, `ping_status`,`post_password`, `post_name`,`to_ping`,`pinged`, `post_modified`, `post_modified_gmt`,`post_content_filtered`,`post_parent`, `guid`,`menu_order`, `post_type`, `post_mime_type`,`comment_count`) VALUES (1, '" + fhoy + "', '" + fhoy + "', '', '" + make + "','', 'publish', 'closed', 'closed','','" + make + "','','', '" + fhoy + "', '" + fhoy + "','', '" + result[0].ID + "','','0', 'dt_links','' ,0);";
        con.query(sql, function(err, result) {
          if (err) throw err;
          resolve(result);
          console.log("1 registro link insertado");
        });
      }).then(function() {
        //new Promise(function(resolve, reject) {
        con.query("SELECT ID FROM wp_posts WHERE post_type='dt_links' ORDER BY ID DESC LIMIT 0,1;", function(err, result, fields) {
          console.log(result);
          var sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (" + result[0].ID + ",'_dool_url', '" + source + "'),(" + result[0].ID + ",'_dool_type','" + text + "'),(" + result[0].ID + ",'_dool_quality','" + quality + "');";
  
          con.query(sql, function(err, result) {
            if (err) throw err;
            console.log("1 registro link meta insertado");
          });
          // });
        });
      });
    }
  });
  </code></pre>