【发布时间】:2017-09-21 22:06:59
【问题描述】:
当节点元素传递给它时,树 api 返回子节点。
我首先传递根节点,然后根据返回的节点递归地传递所有节点,如果它们的 hasChildren 参数为 true。
有没有办法知道函数何时完成创建树。
function recursivelyFetchChildren(id, selectedAsset, parentId){
return ajaxCall(id, selectedAsset, parentId)
.then(function(data){
//collects all childs in childs array
childs.push(data);
for(var i=0; i<data.length; i++){
if(data[i].HasChildren){
return recursivelyFetchChildren(id,selectedAsset,data[i].Id);
}else{
//this return statement prematurely completes the promise
return data[i];
}
}
});
}
recursivelyFetchChildren(id, selectedAsset, parentId).then(function(){
print(childs) //prints the childs before all the promises have resolved
});
谁能提出一种方法,让我可以让 recursivelyFetchChildren 函数等待渲染完整的树?
【问题讨论】:
标签: javascript asynchronous recursion promise