title: When using SQLite3 and ExpressJs how do I make sure app.get() is not asynchronous while calling a db.each() method?
body: <p>I have some front end code that would make a GET request to the url "/api/classes". When triggered my ExpressJs back-end will load my SQLite3 database, then read the query, and finally store the result inside an object and return it. I am using the db.each() method to loop my database. I know my loop works because of the console.log template strings. The problem is when I use PostMan I can see that res.send() gets called immediately, which returns an empty object. How do I make sure res.send() gets called after db.each()? I know db.each() accepts a callback so I tried to place res.send() as an argument, but that did not work. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>const express = require('express');
  let sqlite3 = require('sqlite3').verbose();
  const app = express();
  
  app.get('/api/classes', (req, res) =&gt; {
      let classes = {}
      let db = new sqlite3.Database('./classes.db', (err) =&gt; {
          if (err) {
              console.error(err.message);
          }
          console.log('Connected to the classes database.');
      });   
      let sql = `SELECT CRS_CDE course_code,
                    CRS_TITLE course_name,
                    Column3 start_time,
                    Column4 end_time
              FROM spring_schedule
              WHERE CRS_TITLE IN `;
  
      let classNames = ['Spanish I', 'Accounting II', 'College Algebra']
      let where = '(?' + ',?'.repeat(classNames.length-1) + ')';
      sql += where;
  
      db.each(sql, [...classNames], (err, row) =&gt; {
          if (err) throw err;
          console.log(`${row.course_code} + ${row.course_name} +  ${row.start_time} - ${row.end_time}`);
          classes[row.course_code] = row.course_name
      });
      db.close();
      res.send(classes)
  });
  
  const port = 5000;
  app.listen(port, () =&gt; console.log(`Server started on port ${port}`));
  </code></pre>