jquery: what happens when you pass a deferred object into “then”?

£可爱£侵袭症+ 提交于 2019-12-03 21:09:50

You should build an array and use javascript apply :

//create an array of one deferred per element
var requests =  $.map(A, function(index, a){return= $.ajax(url + "?d=" + a);});
//merge all deferreds into a single one
var mergedRequest = $.when.apply(null, requests);

mergedRequest.done(function(res0, res1, ...){
   //your code goes here
   //if you want results as an array just use arguments
   arguments.length == A.length;
});

EDIT: If you want your calls to be made serially do :

var result = $.ajax(url + "?d=" + A[0]);
for (var i = 1 ; i < A.length ; i++) {
  result = result.pipe(function() {
    return $.ajax(url + "?d=" + a[i]);
  }
}
result.done(processResults).fail(reportFailure);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!