title: node.js + mongodb: wait for query to complete
body: <p>I have a simple code in node.js</p>
  
  <pre><code>async function dbQuery() {
          const MongoClient = require('mongodb').MongoClient; 
          const url = "mongodb://localhost:27017/"; 
          const db = await MongoClient.connect(url);
          const dbo = db.db("mydb");
          const result = await dbo.collection("tblData").find({}).toArray()
          return result;
      }
  
  async function doIt() {
          try {
                const res = await dbQuery();
                console.log("Records: " + res.length);
              } catch (error) {
                console.log(error);
              }
     }
  //
  console.log("Starting...")
  doIt()
  console.log("Done!")
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>Starting...
  Done!
  Records: 24
  </code></pre>
  
  <p>how to force the code to wait for the query to finish? to have the output like:</p>
  
  <pre><code>Starting...
  Records: 24
  Done!
  </code></pre>