我怎样才能在代码中的所需位置等待,直到treeData
上的所有处理完成为止?
var treeData = $scope.rawData;
if ($scope.treeExpanded) {
//expand tree
if (treeData && treeData.children) {
treeData.children.forEach(toggleAll);
}
//how to wait here until all recursion calls are finished?
//console.log(treeData) will show a non modified object equals to $scope.rawData
}
function toggle(d) {
if (d.children) {
d._children = d.children;
d.children = null;
}
}
function toggleAll(d) {
if (d && d.children) {
d.children.forEach(toggleAll);
toggle(d);
}
}
答案 0 :(得分:0)
var treeData = $scope.rawData, callCompleted;
if ($scope.treeExpanded) {
//expand tree
if (treeData && treeData.children) {
callsCompleted = 0;
treeData.children.forEach(toggleAll);
}
//how to wait here until all recursion calls are finished?
//console.log(treeData) will show a non modified object equals to $scope.rawData
}
function toggleCompleted() {
// Do your processing here...
}
function toggle(d, noOfCalls, callBack) {
if (d.children) {
d._children = d.children;
d.children = null;
callsCompleted += 1;
if(noOfCalls === callsCompleted) {
// all calls are completed
callBack();
}
}
}
function toggleAll(d) {
if (d && d.children) {
d.children.forEach(toggleAll);
toggle(d, d.length, trackToggleCompletion);
}
}