title: Node - Wait for loop to finish?
body: <p>When the function below finishes and provides a finalized list of items in the array 'albums', I want it to call another function/do something else with the list. </p>
  
  <p>Currently it posts [] before the function finishes and I know that's because of asynchronous execution, but I thought Node read linearly since it's single threaded?</p>
  
  <pre><code>function getAlbumsTotal(list, params){
      for(var i = 0; i&lt;list.length; i++){
          api.getArtistAlbums(list[i], params).then(function(data) {
              for(var alb = 0; alb&lt;data.body.items.length; alb++){
                  albums.push(data.body.items[alb].id);
              }
          }, function(err) {
              console.error(err);
          });
      }
      console.log(albums);
      //do something with the finalized list of albums here
  }
  </code></pre>