title: Is it ok to initialize exports asynchronously in a node.js module?
body: <p>As MongoDB database access and initialization is asynchronous on Node.js, I would like to define one module per collection that exports wrapped db calls after db initialization.</p>
  
  <p>Such a "Cars.model.js" module looks like that:</p>
  
  <pre><code>var db = require("mongodb");
  db.collection("cars", function(err, col) {
      exports.getCars = function(callback) {
          col.find({}, callback);
      };
  });
  </code></pre>
  
  <p>so that other modules can run:</p>
  
  <pre><code>var carModel = require("Cars.model.js").getCars;
  getCars(err, cars) {
      // (do something with cars here...)
  };
  </code></pre>
  
  <p>It happened to me that <code>getCars</code> was undefined, because db access was not yet initialized at the time my second module was run.</p>
  
  <p>How do you deal with creating such asynchronous db models?</p>