title: Handling callback of asynchronous calls
body: <p>SO.</p>
  
  <p>I have a question in regards to handling Windows Azure Mobile Services asynchronous calls to an Azure database.</p>
  
  <p>Right now, I am posting an object with an array which I am in turn looping over in my WAMS api endpoint.</p>
  
  <p>The loop gets the current object in the array, and pushes it together with an unique ID to the database. This returns the row id from the last inserted row, and in the callback I fetch this as well as instantiate another asynchronous call.<br>
  Here I'm then inserting the last inserted row id, together with a bunch of properties from the object in the current loop of the array.</p>
  
  <p>However, this seems to behave weirdly, and insert the same information in all the columns in my second asynchronous call.</p>
  
  <pre><code>for (var i = 0; i &lt; object.array.length; i++) {
  
      var mssql = request.service.mssql,
          prop1 = object.array[i].prop1,
          prop2 = object.array[i].prop2,
          user  = object.user.userid;
  
      mssql.query("exec stored_procedure ?, ?", [user, prop1], {
          success: function (res) {
  
              var insertedid = res[0];
  
              if (typeof insertedid === 'undefined') {
                  return;
              } else {
                  insertedid = insertedid.ID;
              }
  
              mssql.query("exec stored_procedure_2 ?, ?", [insertedid, prop2], {
  
                  //success
  
              });
  
          }
      });
  }
  </code></pre>
  
  <p>I am probably doing something completely wrong, because I don't quite like what I've produced, but considering my node.js familiarity, this was it.</p>