title: Maintaining order of http requests within a for loop in javascript
body: <p>I am trying to save an array of objects in MongoDB using node.js express and mongoose (MEAN stack). However, when I make the http post requests on the front end for each item in the array, they don't show up on the back end in that order. Here's what I have on the front end:</p>
  
  <pre><code>$scope.postThenPost = function() {
      $http.post('/collection', $scope.doc).success(function(data){
          for (var i = 0; i&lt;$scope.array.length; i++) {
              var req_obj = { thing: $scope.array[i] };
              $http.post('/collection2', req_obj);
          };
      };
  };
  </code></pre>
  
  <p>and on the back end, express runs this for posting into /collection2:</p>
  
  <pre><code>exports.create = function(req, res) {
      var q = new q(req.body.thing);
      q.save(function(err) {
          if (err) {
              return res.status(400).send({
                  message: errorHandler.getErrorMessage(err)
              });
          } else {
              res.jsonp(q);
          };
      });
  };
  </code></pre>
  
  <p>Is there something I can do to ensure that the array goes in to the database in order? Thanks in advance</p>