title: Sequelize JS node updating multiple entities with new data from previous promise
body: <p>I am new to sequelize and NodeJS promises in general. My app basically saves tweets from the Twitter API, but also needs to update some of the saved tweets' data in realtime, like the Retweet count or the Liked count.</p>
  
  <p>But it seems like after fetching the new data, when trying to run all the <strong>update</strong> on my tweet instances, nothing happens. The promise doesn't go through.</p>
  
  <p><strong>To sum up :</strong> I find 100 saved tweets, chain on a callback that fetches their new data from Twitter, and then chain on updating every single 100 tweets with the new data. The later update doesn't go through.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var Sequelize = require('sequelize');
  
  ...
  
  //Getting 100 tweets previously saved in DB
  Sequelize.query("SELECT * FROM tweets WHERE ORDER BY id DESC LIMIT 100", { model: Model }).then(function(result) {
  
      if(result.length == 0) {
          callback(false);
      } else {
          var ids = [];
          var realData = {};
  
          for (var i in result) {
              realData[result[i].dataValues.id_str] = result[i];
              ids.push(result[i].dataValues.id_str);
          }
  
          //getting twitter data for 100 tweets previously saved in DB
          twitVendor.get('statuses/lookup', {
              id : ids.join(',')
          }, function (err, tweets, response) {
              if (typeof err == 'undefined') {
  
                  //to get a synchronous saving of all tweets
                  //this could be cleaned up with a Sequelize.Promise.push(...)
                  var i = 0;
                  var saving = false;
  
                  while (i &lt; tweets.length) {
                      if (!saving) {
                          saving = true;
  
                          console.log('Updating tweet ', tweets[i].id_str);
  
                          //updating tweet with new data from twitter
                          Sequelize.query("UPDATE tweets SET retweet_count = "+tweets[i].retweet_count+", favorite_count = "+tweets[i].favorite_count+" WHERE id_str = '"+tweets[i].id_str+"'", {
                              model: Model
                          }).then(function(result) {
                              console.log('Updated tweet');
                              saving = false;
                              i++;
                          }).catch(function (err) {
                              console.log('Failed to update post ', err);
                              saving = false;
                              i++;
                          });
                      }
                  }
  
                  callback(true);
                  console.log("Updated tweets");
              } else {
                  console.log("Failed :", err);
                  callback(false, err);
              }
          });
      }
  }).catch(function (err) {
      console.log("Failed :", err);
      callback(false, err);
  })</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>EDIT :</strong> If you want to execute the above code, I'd recommend using this <strong>Twit</strong> to hit the Twitter API : <a href="https://github.com/ttezel/twit" rel="nofollow">https://github.com/ttezel/twit</a></p>
  
  <p>To get credentials to hit the API, you will need to set up an app on Twitter though : <a href="https://apps.twitter.com/" rel="nofollow">https://apps.twitter.com/</a></p>
  
  <p><strong>EDIT 2 :</strong> I already tried to use transactions and pure Sequelize functions to make my queries, but the issue stood still.</p>