title: Construct array in nodejs through for loop
body: <p>I am using sails (0.11.0) running on nodejs (6.9.1). I am trying to construct an array by filling it through for loop. I would send this completed array in response to the client. I have tried various methods as suggested by people here on Stack Overflow, for example </p>
  
  <p>the discussion <a href="https://stackoverflow.com/questions/12491101/javascript-create-array-from-for-loop">here</a> suggested </p>
  
  <pre><code>for (var i = yearStart; i &lt; yearEnd+1; i++) {
      arr.push(i);
  }
  </code></pre>
  
  <p>On <a href="https://stackoverflow.com/questions/19084570/how-to-add-items-to-array-in-nodejs">this</a> discussion, it is suggested to use:</p>
  
  <pre><code>var array = calendars.map(function(item) {
      return item.id;
  });
  
  console.log(array);
  </code></pre>
  
  <p>Similarly I tried many methods but I am coming across the same issue that during the loop, the array gets filled but as soon as the loop is completed, the array gets empty because of asynchronous process and therefore I can not send the response. To tackle with this I tried checking the index inside the loop body and send response from inside the loop body itself through </p>
  
  <pre><code>var userArray = [];
  _.each(users, function(user, index){
    MySQLConnector.query('CALL user_image (?)', [user.id], function(err, userImage){
      if(err){
        return res.json({"status":"some_error"});
      }else{
        userID = user.id
        userImageID = userImage[0][0].id;
        var userInfo = {
          userID: userID,
          userImageID: userImageID
        }
        userArray.push(userInfo)
        if(index == users.length - 1){
            res.json({selectedUsers: userArray});
        }
      }
    });
  });
  </code></pre>
  
  <p>I am initiating an empty <strong>userArray</strong> and then iterate through <strong>users</strong> object where each element of the object is characterized by name <strong>user</strong> and an <strong>index</strong>. Through a MySQL query I am fetching the <strong>userImage</strong> object and in each iteration, I am creating an object called <strong>userInfo</strong> that consists of <strong>userID</strong> and <strong>userImageID</strong>. I am pushing this object into <strong>userArray</strong>. And after each iteratio of the for loop (<code>_.each</code>), I check if last index is reached. Once last index is reached, the final array is sent as response before loop body is complete. </p>
  
  <p>Here too I have an issue that the array body is not always completely filled. The reason is due to asynchronous process, the <strong>index</strong> does not always follow the order 0,1,2,3,4,.... and it can start with any number and can jump to any index in the next iteration, for example the first index to start would be 4, the second would be 0, third would be 2 and so on. This sequence would be different for every time we run this for loop. For a user, it will appear to be a total random process. Therefore if <strong>users.length</strong> is 8, and current index is randomly 7 at third iteration, the condition <code>index == users.length - 1</code> will be met and response will be sent just with an array consisting of 3 elements rather than 8. </p>
  
  <p>Can someone suggest me a better and robust way to fill an array through the for loop in nodejs and send that array in response, so that all items are included in the array in their original order? </p>