Given I have a recursive method:
鉴于我有一个递归方法:
tests = {
add: function (data, option, dataSet) {
if(option) {
dataSet.forEach(function (values) {
this.add(values.data, false);
}, this);
}
console.log(data.name)
}
}
And I would call this by using
我会用这个来称呼它
tests.add({name: 'test1'}, true, [{data: {name: 'test2'}}]);
I assume this would log 'test2' before 'test1' in all cases, but in a lot of cases this does not happen. As far I know the forEach function should be blocking the execution of other lines, but this simply does not happen.
我认为在所有情况下都会在'test1'之前记录'test2',但在很多情况下这不会发生。到目前为止,我知道forEach函数应该阻止其他行的执行,但这根本不会发生。
Bit of context: The code should add certain components on a webpage. Those components can depend on other components (which are in dataSet). These dependencies should always be recursively added first.
上下文:代码应该在网页上添加某些组件。这些组件可以依赖于其他组件(在dataSet中)。应始终首先递归地添加这些依赖项。
Any ideas?
1 个解决方案
#1
0
The reason for this inconsistency is that console.log
is not a synchronous function. Read more here
这种不一致的原因是console.log不是同步函数。在这里阅读更多
#1
0
The reason for this inconsistency is that console.log
is not a synchronous function. Read more here
这种不一致的原因是console.log不是同步函数。在这里阅读更多