title: insert data into array from redis inside a foreach in node js
body: <p>I have a node js redis api where I am reading data from redis based on key and if not present fetching it from another api and then inserting this data into redis. Also I am pushing this data into a variable and at the end of the foreach I want to return this value to the end user.</p>
  
  <p>The issue is that redis is running in async and as a result my data variable is always empty. How do i get around this?</p>
  
  <p>Here is the relevant part of my code</p>
  
  <pre><code>   between.forEach(function(entry) {
  
      asyncTasks.push(function(callback){
  
          client.exists(id+entry, function(err, reply) {
              if (reply === 1) {
                  client.get(id+entry, function(err, reply) {
                      var output = JSON.parse(reply);
                      data_output = data_output.concat(output);
                  });
              } else {
                  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
                  var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&amp;fromDate=" + entry + "&amp;toDate=" + entry;
                  request({
  
                      url: slot_url,
                      json: true,
                      headers: headers
                  }, function(error, response, body) {
                      if (!error &amp;&amp; response.statusCode === 200) {
                          var data = [];
                          try {
                              var temp = {};
                              console.log(body);
                              body.data.forEach(function(tempslots) {
                                  temp['date'] = tempslots['date'];
                                  temp['timeslots'] = tempslots['timeslots'];
                                  data = data.concat(temp);
                              });
                              client.set(id+entry, JSON.stringify(data));
                              data_output = data_output.concat(data);
                          } catch (err) {
                              console.log(err.message);
                          }
                      } else {
                          console.log("Something went wrong!! " + error.message);
                      }
                  })
              }
              callback();
          });
      });
  
  });
  
  async.parallel(asyncTasks, function(){
      // All tasks are done now
      result['data'] = data_output;
      result['response'] = 1;
      result['message'] = 'vacancies list fetched successfully!';
      res.json(result);
  });
  </code></pre>
  
  <p>});</p>
  
  <p>the output is always like this <code>{"data":[],"response":1,"message":"Capacity list fetched successfully!"}</code> even though I have checked that the code is fetching and inserting data properly in redis</p>