title: NodeJS MongoDB: How do I get data then returning synchronously
body: <p>I'm a long time programmer (mostly Java) but new to NodeJS/Express.  The biggest problems I'm having are with async operations.  Some of my code is working fine, like when I can do a async database operation then just pass the results to res.render().</p>
  
  <p>In this particular case, I'm in a route handler, and I need to make a call to a function I wrote in the controller to get something from the database, then do something else and then do the render in the route handler.  In other words, I need to run the function then return control with results back to the router.  Everything I have tried is not even compiling, just hangs, or I immediately get back two undefined values.  My current pattern is similar to the accepted solution in <a href="https://stackoverflow.com/questions/59789428/mongodb-with-nodejs-return-data">mongoDB with nodejs return data</a> but I'm not getting my data back.</p>
  
  <p>I've REALLY tried to understand this async/await stuff but it's just not working as I would expect.</p>
  
  <p>The caller in the route handler looks like this:</p>
  
  <pre><code>app.get('/', async (req, res) =&gt; {
      debug("app calling list:");
      const { listInsightErrors, listInsightResults } = await listInsightPeriods();
      debug("app results: %O %O", listInsightErrors, listInsightResults);
      res.render('index', {
          appConfig,
          listInsightErrors,
          listInsightResults
      });
  });
  </code></pre>
  
  <p>The function I'm calling looks like this:</p>
  
  <pre><code>async function listInsightPeriods() {
          var sortKey = { clientId: 1, insightName: 1 };
          var listInsightErrors = {};
          var listInsightResults = {};
          client = await MongoClient.connect(appConfig.mongoUrl);
          const db = client.db(appConfig.mongoDatabase);
          const collection = db.collection('teamInsights');
          let garbage = await collection.find({})
              .project({ 'clientName': 1, 'insightName': 1 })
              .sort(sortKey)
              .toArray((errors, results) =&gt; {
                  if (errors) {
                      debug('listInsightPeriods find error: %O', errors);
                  } else {
                      debug('listInsightPeriods got results: %O', results);
                  }
                  listInsightErrors = errors;
                  listInsightResults = results;
                  debug("closed, returning %O %O", listInsightErrors, listInsightResults);
                  return { listInsightErrors, listInsightResults };
                  debug("Passed inside return");
              });
          debug("Outside of db call");
          return { listInsightErrors, listInsightResults };
      }
  </code></pre>
  
  <p>What happens is listInsightPeriods gets the data just fine, but the route handler doesn't get it, and I see the debugging output from the route handler before I see the debugging output from listInsightPeriods.  So I don't feel the route handler is waiting for the data to return.</p>
  
  <p>Any help you can offer would be great.  I've read so many pages on this and I still can't get it. Thanks.</p>