title: NodeJS and Express - Exports function skips over SQLite3 DB query
body: <p>Trying to link a function in a separate JS file to my index, but part of the function is skipped over, returns to the index.js app.post method, and the finally returns back to the db query it skips over in the other JS file.</p>
  
  <p>sales.js = </p>
  
  <pre><code>const sqlite3 = require('sqlite3').verbose()
  
  const db = new sqlite3.Database('./database/stock.db', (err) =&gt; {
      if (err) return console.error(err.message)
      console.log('Connected to the "stock.db" SQlite database.')
  })
  
  module.exports = {
  
  sale: function(formData, cartlist) {
      const item = parseInt(formData, 10)
      console.log(item)
      const sql = "SELECT name, price FROM products WHERE barcode_id = " + item + ";"
      console.log(sql)
      console.log("test1")
      db.all(sql, (err, data) =&gt; {
          if(err) console.error(err.message)
          console.log("test2")
          cartlist = cartlist.concat(data)
          console.log(cartlist)
      })
      console.log("test3")
      return cartlist
  }
  }
  </code></pre>
  
  <p>index.js (relevant parts) =</p>
  
  <pre><code>const sales = require('./sale.js')
  </code></pre>
  
  <p>.
  .
  .
  .</p>
  
  <pre><code>var cartlist = []
  
  app.post('/sales', async(req, res) =&gt; {
      const formData = String(req.body.Barcode)
      cartlist = await sales.sale(formData, cartlist)
      console.log(cartlist)
      res.render('cart', {purchases: cartlist})
  })
  </code></pre>
  
  <p>.
  .
  .</p>
  
  <p>I have the tests there to show order and test1 gets logged first, followed by test3, and then back to test2, showing it jumps out, then back in again. Not familiar with SO formatting or Node.js so apologies in advance. I imagine this somewhat involves callback functions, but I have yet to fully understand how they work.</p>