javascript - Controlling AJAX requests -


is there anyway halt additional ajax requests , fire them 1 @ time? have printing application need process each request before moving on next. in code have loop iterating on average of 15 times make ajax call each iteration , process response $.when() ready. problem each response gets processed returned, not when previous 1 has finished. i'm trying avoid making calls synchronous, there anyway process these responses 1 @ time?

yes, store promise in original array, loop on original array again pass $.when. callback have arguments in order.

var mydata = [   {     name: 'apple',     color: 'red'   },   {     name: 'orange',     color: 'orange'   } ];  mydata.foreach(function (obj) {   obj.promise = $.get('/object-info', obj); });  $.when.apply($, mydata.map(function (obj) {   return obj.promise; })).then(function () {   var args = array.prototype.slice.call(arguments);   args.foreach(function (result) {     console.log(result); // results in order   }) }); 

// *** mock    $.get = function (url, data) {      var defer = $.deferred()      settimeout(function () {          defer.resolve(data.color);      }, 1500);      return defer.promise();  };    // *** end mock    var mydata = [    {      name: 'apple',      color: 'red'    },    {      name: 'orange',      color: 'orange'    }  ];    mydata.foreach(function (obj) {    obj.promise = $.get('/object-info', obj);  });    $.when.apply($, mydata.map(function (obj) {    return obj.promise;  })).then(function () {    var args = array.prototype.slice.call(arguments);    args.foreach(function (result) {      console.log(result); // results in order    })  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Comments