title: Return value from a mongodb query from nodejs
body: <p>EDIT</p>
  
  <p>OK I read <a href="https://stackoverflow.com/questions/10676013/return-value-from-a-mongodb-call-from-nodejs">here</a>."You can't usefully return with asynchronous functions. You'll have to work with the result within the callback. This is due to the nature of asynchronous programming: "exit immediately, setting up a callback function to be called sometime in the future. And, at least with the current standard of ECMAScript 5, you can't get around this. As JavaScript is single-threaded, any attempt to wait for the callback will only lock up the single thread, keeping the callback and the return user forever pending in the event queue."</p>
  
  <p>Is this still the case today?</p>
  
  <p>ORIGINAL QUESTION</p>
  
  <p>I have a problem accessing my variable outside the function in my node.js application.</p>
  
  <pre><code>const url = "mongodb://localhost:27017/";
  getAllSampleTypes();
  
  // I would like to have the variable "requested" accessible here
  
  function getAllSampleTypes() {
      MongoClient.connect(url, function (err, db) {
          var dbo = db.db("myDb");
          dbo.collection("data").distinct("sample_type", {}, (function (err, requested) {
  // variable "requested" is accessible here
  
              })
          );
  
      });
  }
  </code></pre>
  
  <p>I tried with async/await but I still have the same problem.</p>
  
  <pre><code>function getTypes() {
      MongoClient.connect(url, async function (err, db) {
          let dbo = db.db("myDb");
          return await dbo.collection("data").distinct("sample_type", {});
  
      });
  }
  console.log(getTypes()); //Promise { undefined }
  </code></pre>