title: Lambda function completes, but query to RDS is incomplete
body: <p>I was trying to make a post query the db (RDS) using handler.async. 
  However, I ran into the following issues.</p>
  
  <p>Half of the time, the lambda function completes but the query is not successfully sent to RDS. The other half of the time, it will be completely send to lambda. Tried adding a setTimeout function to increase lambda execution time by 3 secs and the query will be sent all the time. </p>
  
  <p>Also the log will shows the error:</p>
  
  <blockquote>
    <p>INFO  Error: Cannot enqueue Query after fatal error.</p>
  </blockquote>
  
  <p>The following are my code: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>var mysql = require('mysql');
  var connection = mysql.createConnection({
    host     : '***',
    user     : '***',
    password : '***',
    database : '***'
  });
  
  
  exports.handler = async (event) =&gt; {
  const sql = `INSERT INTO forms VALUES(777,2,3,4,5,6,7,8,9,10,11);`;
  const query = (x) =&gt; {
    return new Promise ((resolve,reject)=&gt;{
      resolve(connection.query(x, function (error, results, fields) {
        console.log(error)
        console.log(results)
        console.log(fields)
        
  }))})}
  await query(sql)
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>With the timeout function,</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>var mysql = require('mysql');
  var connection = mysql.createConnection({
    host     : '***',
    user     : '***',
    password : '***',
    database : '***'
  });
  
  
  exports.handler = async (event) =&gt; {
  const sql = `INSERT INTO forms VALUES(777,2,3,4,5,6,7,8,9,10,11);`;
  const query = (x) =&gt; {
    return new Promise ((resolve,reject)=&gt;{
      resolve(connection.query(x, function (error, results, fields) {
        console.log(error)
        console.log(results)
        console.log(fields)
        
  }))})}
  await query(sql)
  
  await wait(3000)
  
  }
  
  
  
  const wait = (x) =&gt; {
    return new Promise ((resolve,reject)=&gt;{
      setTimeout(()=&gt;{resolve(console.log("delay"))}, x);
    })
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The first value is a primary key. A constant 777 is sent to check, if error shows duplicate primary key, it will mean that the query is successfully sent. If there is no error, it means that the query is unsuccessfully sent although lambda finishes. </p>
  
  <pre><code>execution result succeeded but shows:
  START RequestId: e541fe4b-6927-4fbb-90b4-750f77e5f460 Version: $LATEST
  2019-12-19T01:54:45.212Z    e541fe4b-6927-4fbb-90b4-750f77e5f460    INFO    Error: **Cannot enqueue Query after fatal error**.
      at Protocol._validateEnqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:212:16)
      at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:138:13)
      at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:201:25)
      at /var/task/index.js:14:24
      at new Promise (&lt;anonymous&gt;)
      at query (/var/task/index.js:13:10)
      at Runtime.exports.handler (/var/task/index.js:20:7)
      at Runtime.handleOnce (/var/runtime/Runtime.js:66:25) {
    code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
    fatal: false
  }2019-12-19T01:54:45.213Z   e541fe4b-6927-4fbb-90b4-750f77e5f460    INFO    undefined2019-12-19T01:54:45.213Z   e541fe4b-6927-4fbb-90b4-750f77e5f460    INFO    undefined2019-12-19T01:54:45.262Z   e541fe4b-6927-4fbb-90b4-750f77e5f460    INFO    delayEND RequestId: e541fe4b-6927-4fbb-90b4-750f77e5f460
  REPORT RequestId: e541fe4b-6927-4fbb-90b4-750f77e5f460  Duration: 51.09 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 80 MB  
  </code></pre>
  
  <p>May you please advise and also tell me which is the best way to execute it??</p>