【发布时间】:2023-04-08 13:53:01
【问题描述】:
如何在代码中的所需位置等待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);
}
}
【问题讨论】:
-
使用回调函数。您可以访问此link 了解回调。
-
看看promises on MDN。不过不支持 IE。
-
@Pingbeat @Shubham 我看了一下回调的概念。但是由于
toggleAll()的嵌套递归调用,我很难对我的示例采用回调方法。应该给哪个函数回调,什么时候执行回调?
标签: javascript