title: UnhandledPromiseRejectionWarning for pg.Pool promise chaining
body: <p>My promise returns the expected database query from the first <code>.then</code> statement, but the second <code>.then</code> in the chain fails with <code>UnhandledPromiseRejectionWarning: ReferenceError: client is not defined</code>. As far as I can tell, <code>client</code> is defined and each promise has a valid <code>.catch</code>. What am I doing wrong here?</p>
  
  <pre><code>const pool = new pg.Pool(config.db);
  
  pool.connect()
    .then(client =&gt; {
      return client.query('SELECT * FROM staffers WHERE id=800')
        .then(res =&gt; {
          client.release();
          console.log(res.rows[0]);
        })
        .catch(err =&gt; console.error('Error executing first psql query', err.stack));
    })
    .then(client =&gt; {
      return client.query('UPDATE staffers SET num_times_tweeted=num_times_tweeted + 1 WHERE id=800')
        .then(res =&gt; {
          client.release();
          console.log(res);
        })
        .catch(err =&gt; console.error('Error executing second psql query', err.stack));
    })
    .catch(err =&gt; {
    client.release();
    console.error('Error acquiring client: ', err.stack);
  });
  
  // pool shutdown
  pool.end();
  </code></pre>
  
  <p>The specific error I get is as follows:</p>
  
  <pre><code>(node:44233) UnhandledPromiseRejectionWarning: ReferenceError: client is not defined
      at pool.connect.then.then.catch.err (/Users/danielmiller/.../.../app.js:...)
      at process._tickCallback (internal/process/next_tick.js:68:7)
  (node:44233) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:44233) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  
  </code></pre>
  
  <p><strong>EDIT</strong>
  Based on Chris's suggestion I was able to get the promise works by returning the <code>client</code> from each <code>.then</code> statement. I don't understand why this works though? Also calling <code>client.release();</code> within <code>finally</code> doesn't seem to actually release <code>client</code>?</p>
  
  <pre><code>pool.connect()
  
      .then(client =&gt; {
          return client.query('SELECT * FROM staffers WHERE id=800')
              .then(res =&gt; {
                  console.log(res.rows[0]);
                  return client;
              })
              .catch(err =&gt; console.error('Error executing first psql query', err.stack));
      })
  
      .then(client =&gt; {
          return client.query('UPDATE staffers SET num_times_tweeted=num_times_tweeted + 1 WHERE id=800')
              .then(res =&gt; {
                  console.log(res);
                  return client;
              })
              .catch(err =&gt; console.error('Error executing second psql query', err.stack));
      })
      .catch(err =&gt; {
          client.release();
          console.error('Error acquiring client: ', err.stack);
      })
      .finally(() =&gt; {
          client.release();
          // pool shutdown
          pool.end();
      });
  </code></pre>