jQuery Deferred - waiting for multiple AJAX requests to finish -


this question has answer here:

i have 3 layer deep chain of deferred ajax calls, , ideally going kick promise way when deepest layer finishes (makes me thing of inception... "we need go deeper!").

the problem i'm sending off many ajax requests (possibly hundreds) @ once , need defer until of them done. can't rely on last 1 being done last.

function updateallnotes() {     return $.deferred(function(dfd_uan) {         getcount = 0;         getreturn = 0;         (i = 0; <= index.data.length - 1; i++) {             getcount++;             $.when(getnote(index.data[i].key)).done(function() {                 // getnote deferred                 getreturn++             });         };         // need here         // when getreturn == getcount, dfd_uan.resolve()     }).promise(); }; 

you can use .when(), , .apply() multiple deferred. extremely useful:

function updateallnotes() {     var getarray = [],         i, len;      (i = 0, len = data.length; < len; += 1) {         getarray.push(getnote(data[i].key));     };      $.when.apply($, getarray).done(function() {         // things need wait until gets done     }); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -