[英]How to debug this Node script?
我正在尝试执行以下代码,但未获得预期的输出。
recursive(prodURL, function(err, files) {
targetDeviceUpdate("updatedPath");
});
function targetDeviceUpdate(sourceImages, updatedPath) {
console.log("1 " + updatedPath);
recursive(prodURL + "/device", function(err, files) {
console.log("3 " + updatedPath);
});
}
预期产量:
1个更新路径
3更新路径
1个更新路径
3更新路径
实际输出:
1个更新路径
1个更新路径
3更新路径
3更新路径
那么,您想要什么功能? 您的代码完全按照您说的去做,因为它从第二行再次调用了函数[self]。 然后终于到了剩下的。
如果希望它们交织在一起,则必须在函数中顺序调用它们,而不是递归地调用它们。 递归操作会尾随,最终呈螺旋状,这会导致您看到这种重复。
请尝试使用描述性名称(更长的名称实际上更好),尽管CamelCase不错,但我更喜欢将连字符用于命名变量,但是,由于javascript不会将连字符解释为负号,因此您可以使用underscores_to_name_your_variables_cleanly。
the_method_thatinvokes_target_device_update(prodURL, function(err, files) {
target_device_update("updatedPath");
});
function target_device_update(sourceImages, updated_path) {
console.log("Here is the first line: " + updated_path);
the_method_that_invokes_target_device_update(prodURL + "/device", function(err, files) {
//invoking this function will naturally call the first line again before reaching the rest of the code.
//and this 'callback' or 'remainder' code gets called only after, and twice at that.
console.log("3 " + updated_path);
});
}
为了获得想要的效果,您不需要递归。 只需依次调用更改即可。 您到底要达到什么目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.