title: Q.js variables passing in parallel flows
body: <p>While implementing promises got this code:</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient
  MongoClient.connect(db_uri, function(err, db) {
      if(err) throw err;
      var ccoll = db.collection('cdata');
      app.locals.dbstore = db;
  }
  var json= {}
  //Auth is a wrapped mongo collection
  var Auth = app.locals.Auth;
  var coll = app.locals.dbstore.collection('data');
  var ucoll = app.locals.dbstore.collection('udata');
  var ccoll = app.locals.dbstore.collection('cdata');
  var Q = require('q');
  //testing with certain _id  in database
  var _id = require('mongodb').ObjectID('530ede30ae797394160a6856');
  //Auth.getUserById = collection.findOne()
  var getUser = Q.nbind(Auth.getUserById, Auth);
  //getUserInfo gives a detailed information about each user
  var getUserInfo = Q.nbind(ucoll.findOne, ucoll);
  var getUserData = Q.nbind(ccoll.findOne, ccoll);
  //"upr" is a group of users
  //getUsers gives me a list of users, belonging to this group
  var getUsers = Q.nbind(ucoll.find, ucoll);
  //Auth.getUserById = collection.find()
  var listUsers = Q.nbind(Auth.listUsers, Auth);
  var uupr = {}
  var cupr = {}
  getUserInfo({_id:_id})
  
      .then(function(entry){
          console.log('entry:', entry);
          uupr = entry;
          var queue = [getUsers({upr:entry.name}), getUserData({_id:entry._id})]
          return Q.all(queue);
      }
  )
      .then(function(array2){
          console.log('array2:', array2);
          cupr = array2[1]
          var cursor = array2[0]
          var cfill =  Q.nbind(cursor.toArray, cursor);
          return cfill();
      }
  )
      .then(function(data){
          json = {data:data, uupr:uupr, cupr:cupr}
          console.log('json:', json)
          res.render('test', {json : JSON.stringify(json)})
      }
  )
  </code></pre>
  
  <p>Its work can be described by a diagram:</p>
  
  <pre><code>getUserInfo()==&gt;(entry)--+--&gt;getUsers()=====&gt;array2[0]--+--&gt;populate user list===&gt;data---&gt;render
                           |                              |
                           +--&gt;getUserData()==&gt;array2[1]--+
  </code></pre>
  
  <p>I've used external variables <code>uupr</code> and <code>cupr</code> to store data from first <code>.then</code> calls.</p>
  
  <p>So I have two problems:</p>
  
  <p>1) Avoid using external variables.</p>
  
  <p>2) rearrange code to get alternative flow diagram.</p>
  
  <pre><code> getUserInfo()==&gt;(entry)--+--&gt;getUsers()==&gt;usersList--&gt;populate user list==&gt;usersData-+-&gt;render
                            |                                                           |
                            +--&gt;getUserData()====&gt;uprData-------------------------------+
  </code></pre>
  
  <p>Any advice is appreciated</p>