我想使用 $.deferred 对象来处理我的递归函数的请求。
但在这里我有一个问题
$ .when不会等待call1 成功然后做 call2!
它不会将返回的数据从 call1 发送到 call2 函数!
注意:只想用 async:true 实现我的 ajax;
提前致谢!
m.mov
//*********************************************************************
var i = 3; // just run the 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 which receive data from call1 and call some $.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 for "+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;
}