title: Chaining Functions Does not Work Synchronously
body: <p>I query the database as follows:</p>
  <pre><code>const testMe = async () =&gt; {
      const index = await _fetchNextIndex()
  
      if (index === undefined) {
          throw new Error('Next index could not fetched')
      }
  
      return 'Success'
  }
  
  
  const _fetchNextIndex = async () =&gt; {
      return db.query(&quot;SELECT nextval(pg_get_serial_sequence('testme_table', 'id'))&quot;, (err, result) =&gt; {
          if (err) {
              throw new Error('Index could not generated')
          }
          return result.rows[0]
      })
  }
  </code></pre>
  <p><code>db</code> is defined as:</p>
  <pre><code>const {Pool} = require('pg')
  const pool = new Pool()
  
  module.exports = {
      query: async (text, params, callback) =&gt; {
          return await pool.query(text, params, callback)
      }
  }
  </code></pre>
  <p>My code doesn't w work synchronously. <code>index</code> is undefined and error is thrown. However, when I debug the application I see that this line is called after a time later:</p>
  <pre><code>return result.rows[0]
  </code></pre>
  <p>What I miss?</p>
  <p><strong>EDIT 1:</strong> I've added <code>async</code> and <code>await</code> into <code>query</code> function.</p>
  <p><strong>EDIT 2:</strong> I've also removed the callback.</p>
  <pre><code>const {Pool} = require('pg')
  const pool = new Pool()
  
  module.exports = {
      query: async (text, params) =&gt; {
          return await pool.query(text, params)
      }
  }
  </code></pre>