title: Best practice to hang on to variables when using Promises
body: <p>I am new to Promises and I was wondering what the best practice is to keep variables while going down the chain?</p>
  
  <p>Connecting to MongoDB via Promise is pretty straight forward:</p>
  
  <pre><code>connectToMongoDB(data).done(function(db) {
  
      var collection = db.collection('test_inserts');
      // do more stuff here
  
  });
  </code></pre>
  
  <p>But what happens if I have to connect to two different databases?</p>
  
  <pre><code>connectToMongoDB1(data1).then(function(db1) {
  
      return connectToMongoDB2(data2);
  
  }).done(function(db2) {
  
      var collection = db1.collection('test_inserts');
      // ERROR: db1 is undefined
  
  });
  </code></pre>
  
  <p>That error makes perfect sense. But how do I forward <code>db1</code> without altering my <code>connectToMongoDB2()</code> function, because I want to keep <code>connectToMongoDB2()</code> and all my promises in general very generic?</p>
  
  <p>I mean, I could wrap an object around that stores all the relevant stuff, but that looks kind of hacky:</p>
  
  <pre><code>var relevantStuff = {};
  
  connectToMongoDB1(data1).then(function(db1) {
  
      relevantStuff.db1 = db1;
      return connectToMongoDB2(data2);
  
  }).done(function(db2) {
  
      var collection = relevantStuff.db1.collection('test_inserts');
      // do more stuff here
  
  });
  </code></pre>
  
  <p><strong>What is the best practice?</strong></p>