title: Push asynchronous response into a array in NodeJS
body: <p>I have an array of elements through which I'am iterating and making some RPC calls to get the response, push the response into array and send it back to UI to display. But these responses are asynchronous and not getting pushed into array. So when the response is received at UI side, it is empty.</p>
  
  <p>NodeJS code:</p>
  
  <pre><code>app.get('/getData/:obj', function(req,res, next){
  
      data = JSON.parse(req.params.obj);
  
      var options = ["location","start","end","user"];
  
      /*Data looks like this*/
  
      /*[ { start: '12-05-2016' },
        { end: '12-09-2016' },
        { location: [ 'US','UK' ] },
        { user: [ 'Jim','Jack' ] } ]*/
  
      var requestArray = [];
      for(var d in data) {
          if(options.indexOf(d) &gt; -1 ) {
              var rpcRequest = {};
              rpcRequest[d] = data[d]
              requestArray.push(rpcRequest);
          }
  
      }
      console.log(requestArray);
      var resp = []
  
      for(var i in requestArray)    {
          var item = requestArray[i];
          _.each(item, function(value, key) {
              console.log('key :'+key+'\tvalue: '+value);
              if(util.isArray(value)) {
                  for(var val in value)   {
                      var request = {};
                      request[key] = value[val];
                      console.log('\tkey :'+key+'\tvalue: '+value[val]);
                      console.log('request: '+JSON.stringify(request));
  
                      switch(key) {
                          case "location": 
                              rpcClient.location(request, function(err, response) {
  
                                  response = (JSON.stringify(response)); //asynchronous  response
  
                                  resp.push(response); //push response into array
  
                              });
                              break;
                          case "user":
                              rpcClient.user(request, function(err, response) {
  
                                  response = JSON.stringify(response); //asynchronous  response
  
                                  resp.push(response); //push response into array
  
                              });
                              break;
  
                          default: break;
                      }
  
  
                  }
              }
  
          });
  
      }
  
      console.log(resp);
      res.send({d:resp}); // send the response array, but it is empty
  }); 
  </code></pre>
  
  <p>How do I make the pushing of data into array synchronous or get the proper data in some way. I have heard of async module, nut not quite sure how to use it here. </p>
  
  <p>Seeking help! Thank you :)</p>