title: NodeJS: Simplest way to wait for a for loop
body: <p>I am trying to find the simplest way to implement a way to wait for a for loop to finish on Node. My setup is NodeJS + ExpressJS + SequelizeJS. </p>
  
  <p>The following example is using a route:</p>
  
  <pre><code>router.get('/today', function(req, res, next){
  
    /* Get all created rows today */
    models.prices.findAll({
      order: [['createdAt', 'DESC']],
      limit: 10,
      where: Sequelize.where(
        Sequelize.fn('DATE', Sequelize.col('createdAt')),
        Sequelize.literal('CURRENT_DATE')
      )
    }).then(function(changes){
  
  
      /* Get price difference per sourceId &amp; productId */
      for (let i = 0; i &lt; changes.length; i++) {
        models.prices.findOne({
          where: {
              productId: changes[i].productId,
              sourceId: changes[i].sourceId
          },
          order: [['createdAt', 'DESC']],
          limit: 1,
          offset: 1
        }).then(function(difference){
          /* Add price difference to array */
          changes[i]['difference'] = (changes[i].price - difference.price).toFixed(2);
        });
      }
  
  
      /* RETURN updated changes array here? */
  
    }).then(function(changes){
      res.render('today', {
        changes: changes
      });
    });
  });
  </code></pre>
  
  <p>I am fairy new at Async/Await, so I am struggling finding a way to implement it into this Express route (assuming that's my only option) - because it seems you can't just do a then on a loop since it's not a promise. <code>/* RETURN updated changes array here? */</code> is where I'd return the changes, however if I return the changes there it obviously returns the un-updated changes because it didn't wait for the for loop. </p>
  
  <p>This route is taking the rows of price changes for the day, then getting the difference in prices from previous rows and adding them to the array. Afterwards it should push that modified array to the last .then and from there go to my template.</p>
  
  <p>I see a lot of options here, but it started to get messy. So I wanted some suggestions from here.</p>