title: node/express.js - forEach loop with request to API pushes result to array, but array remains empty
body: <p>Here is a snippet from my express.js file:</p>
  
  <pre><code>function playlistTracks(req, res) {
  var tracks = [];
  var genres = ['hi'];
  
  var playlistOption = {
      url: 'https://api.spotify.com/v1/users/' + req.params.user + '/playlists/'+ req.params.id + '/tracks',
      headers: { 'Authorization': 'Bearer ' + access_token },
      json: true
  };
  
  rp(playlistOption)
      .then(function (body) {
          tracks = body;
          return tracks;
      })
      .then(function (tracks) {
          getGenre(tracks);
          res.render('tracks', {
              data: tracks
          });
      })
      .catch(function (err) {
          console.log('couldnt get tracks' , err);
          throw err;
      });
  
  function getGenre(tracks) {
      tracks.items.forEach(function(e){
          var reqGenre = {
              url: 'https://api.spotify.com/v1/artists/' + e.track.album.artists[0].id,
              json: true
          };
  
          rp(reqGenre)
              .then(function(body) {
                  genres.push(body.genres)
              })
              .catch(function(err){
                  console.log('couldnt get genres' , err);
                  throw err
              });
      });
  
      io.emit('playlists', {genres: genres});
      console.log(genres) // &lt;---empty 
  }
  </code></pre>
  
  <p>}</p>
  
  <p>the "getGenre" function is where I have the most trouble with. I want to know how I can update the "genres" array with that function. I've tried a couple of solutions but can't seem to get my head around the async nature of a request. </p>
  
  <p>I've looked at <a href="https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call">this</a> and other solutions already, but can't figure out how to apply them to my code.</p>
  
  <p>I'm using <a href="https://github.com/request/request-promise" rel="nofollow noreferrer">request-promise</a> to get the api request.</p>