javascript - deferred for chaining ajax wont work -
i wanna using $.deferred objects handle recursive function's requests.
but here have problem
$.when wont wait call1 success call2!
it wont send returned data call1 call2 function!
note : want implement ajax's async:true;
thanks in advance!
m.mov
//********************************************************************* var = 3; // run queue 3 times function getnode(node_object_array) { $.when(call1(node_object_array)).then(call2); i--; if(i >= 0) getnode(next_level_childs); } function call1(node_object_array) { var root_array = new array(); var d = new $.deferred(); $.each(node_object_array , function(index , each_root_node) { console.log("making request for"+each_root_node.node_guid ); $.ajax({ url: url , datatype: 'json', success: function(json) { root_array.push(getnodedata(json)); console.log("success request for"+each_root_node.node_guid ); }, }); }); d.resolve(root_array); return d; } //****** call2 receive data call1 , call $.ajax's **** function call2(data) { var next_level_childs = new array(); var d = new $.deferred(); $.each(data , function(index , each_root_node) { $.each(each_root_node.childs , function(index , each_root_node_child) { console.log("making request "+each_root_node_child ); $.ajax({ url: url , datatype: 'json', async : false, success: function(json) { console.log("success request for"+each_root_node_child ); next_level_childs.push(getnodedata(json)); } }); }); }); d.resolve(next_level_childs); return d; }
if resolve deferred before ajax call completes, $.when consider finished , call next function (call2). need resolve on success/fail of ajax call inside call1.
edit: need combine promises ajax calls resolve deferred after of them have completed. noticed $.each call.
Comments
Post a Comment