title: aws lambda and promise-mysql
body: <p>i've encountered an issue using the promise-mysql library within aws lambda.</p>
  
  <p>this is for upgrade the lambda functions from node 4.3 to node 10.x   and is using "promise-mysql": "^4.1.0"</p>
  
  <p>the code does something like this...</p>
  
  <pre><code>mysql.createConnection({
      host : RdsHost,
      user : RdsUser,
      password : RdsPassword,
      database : RdsDatabase
  }).then((conn) =&gt; {
      connection = conn;                      // defined earlier
      return connection;
  })
  .then(() =&gt; {
      var ffff = new FFFF(connection);        // passes in connection
      return ffff.getRows();                  // runs query via connection
  })
  .then((rows) =&gt; {
      ...
  })
  .then(() =&gt; {
      var pppp = new PPPP(connection);        // passes in connection
      return pppp.getRows();                  // runs query via connection
  })
  .then((rows) =&gt; {
      xxxx = rows;
  })
  .then(() =&gt; {
      if(connection) connection.end();        // &lt;- if timeout failure does NOT fall over into .catch
      console.log('Result is ', xxxx);
      return context.succeed(xxxx);
  })
  .catch(function(err){
      if(connection) connection.end();
      console.log(err);
      return context.fail(new Error(configuration.ApiGatewayErrorRegex + 'Could not process the query' + err));
  });
  </code></pre>
  
  <p>there is a keybounce issue on the screen which i will eventually but cannot right now.</p>
  
  <p>the lambda runs and gets a result but when the .end() is called it returns the following errors:</p>
  
  <pre><code>'ERROR Unhandled Promise Rejection'
  'Error: Quit inactivity timeout'
  'PROTOCOL_SEQUENCE_TIMEOUT'
  </code></pre>
  
  <p>this was happening in previous version of the code (node4.3) but was not flooding the monitoring with errors and rds alerts.</p>
  
  <p>in order to reduce the errors and rds alerts i've had to do something like the following...</p>
  
  <pre><code>.then(() =&gt; {
      if(connection)  return new Promise( ( resolve, reject ) =&gt; {
          connection.end( err =&gt; {
              if ( err ) return reject( err );
              resolve();
          }).then(() =&gt; {
              console.log('Result is (e):', xxxx);
              return context.succeed(xxxx);
          });
      });
      //fall-through
      console.log('Result is:', xxxx);
      return context.succeed(xxxx);
  })
  .catch(function(err){
      if(connection)  return new Promise( ( resolve, reject ) =&gt; {
          connection.end( err =&gt; {
              if ( err ) return reject( err );
              resolve();
          }).then(() =&gt; {
              console.log(err);
              return context.fail(new Error(configuration.ApiGatewayErrorRegex + 'Could not process the query' + err));
          });
      });
      //fall-through
      console.log(err);
      return context.fail(new Error(configuration.ApiGatewayErrorRegex + 'Could not process the query' + err));
  });
  </code></pre>
  
  <p>which seems unsatisfactory considering i would have thought the .catch() would have managed this condition.<br>
  [ed: further testing shows it makes virtually no difference anyway].</p>
  
  <p>part of the issue is that the code needs to return a context.succeed or context.failure   and so the promise stuff has become extremely verbose to accommodate this.</p>
  
  <p>i have tried .destroy() instead of .end() and this did work as .destroy() is not returning a promise. however i wanted to keep the code as close to the original version as possible.</p>
  
  <p>[ed] i have also tried (suggested elsewhere) doing
  connection.end(function(err){})
  or similar but this also did not work. </p>
  
  <p>has anyone had similar issues and if so did you have a better work around?</p>