title: Node.js singleton module pattern that requires external inputs
body: <p>normally we might create a simple singleton object like so with Node.js:</p>
  
  <pre><code>var foo = {};
  
  module.exports = foo;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>function Foo(){}
  
  module.exports = new Foo();
  </code></pre>
  
  <p>however</p>
  
  <p>what is the best way to make a clean singleton module that needs an external variable for initialization? I end up making something like this:</p>
  
  <pre><code>var value = null;
  
  function init(val){
  
   if(value === null){
      value = val;
    }
  
    return value;
  }
  
  module.exports = init;
  </code></pre>
  
  <p>this way someone using the module can pass in an initializing value for a certain variable. Another way to do it would be like so:</p>
  
  <pre><code>function Baz(value){
  this.value = value;
  }
  
  var instance = null;
  
  module.exports = function init(value){
  
  if(instance === null){
      instance = new Baz(value);
  }
     return instance;
  
  }
  </code></pre>
  
  <p>there's two problems I encounter with this:</p>
  
  <p>(1) this is minor, but the semantics is wrong. We can rename init to getInstance, but we can't make the same function literal mean "initialize and get" since they are different meanings. So we have to have a function that does two different things. Create an instance and retrieve and instance. I don't like this especially since in some cases we need to make sure the argument to initialize the instance is not null. With multiple developers using a module it's not clear if a module has been initialized yet, and if they pass in undefined into the module that hasn't been initialized, that could become a problem or just confusing at the least.</p>
  
  <p>(2) this is more important - in some cases initializing Baz is asynchronous. For example, making a Redis connection or reading from a file to initialize a singleton, or making a socket.io connection. This is what really trips me up. </p>
  
  <p>e.g. here is a module that I have that I consider really ugly that stores a socket.io connection:</p>
  
  <pre><code>    var io = null;
  
      var init = function ($io) {
  
          if (io === null) {
  
              io = $io;
  
              io.on('connection', function (socket) {
  
                  socket.on('disconnect', function () {
  
                  });
  
              });
          }
  
          return io;
      };
  
  module.exports = {
      getSocketIOConn: init
  };
  </code></pre>
  
  <p>the above module is initialized like so:</p>
  
  <pre><code>var server = http.createServer(app);
  var io = socketio.listen(server);
  require('../controllers/socketio.js').getSocketIOConn(io);
  </code></pre>
  
  <p>So I am looking for a design pattern that allows us to create a singleton module where the initialization process is asynchronous. Ideally we won't have the same function both initializing the instance as well as retrieving it. Does such a thing exist?</p>
  
  <p>I don't think there is necessarily a way to create a pattern that solves this problem but perhaps I am making the mistake of structuring my code in a way that is creating a problem that doesn't need to exist- the problem of initializing a module with a value only once, but using one function to both init the instance and retrieve the instance.</p>