【发布时间】:2020-10-11 09:23:10
【问题描述】:
这是我尝试对包含许多文件的程序进行静态分析,所有文件都具有引用程序中其他文件的导入语句,以查找循环引用。 dependenciesCollection 是 [key, value] 条目的映射,文件名作为键,它们的依赖文件数组作为值。此代码在 node.js v12 中运行,我需要 async/await 为节点提供垃圾收集的机会,否则它会因堆栈溢出而崩溃。我无法让函数返回堆栈以返回调用函数并继续调用它的 for 循环。我做错了什么?
let cyclicReferences = [] // a global array of pathToken arrays
const parseCollection = async (currentPathTokens, fileName, dependenciesCollection, targetFile, report) => {
// currentPathTokens has entries for the sequence of entries leading to this file
let dependencies = dependenciesCollection.item(fileName) // get array of dependent file names to investigate
// Some testing code here omitted for brevity
for (let index = 0; index < dependencies.length; index++) {
let nextFile = dependencies[index]
currentPathTokens.push(nextFile) //traveled path so far
await parseCollection(currentPathTokens, nextFile, dependenciesCollection, targetFile, report)
// I want the called recursive function to return to continue the for loop here, but it does not.
currentPathTokens.pop() // shorten the path to dependency's parent node
}
// At end of this for loop, return to the caller to continue the loop there.
// Never happens! Always goes back to the very first caller, never an intermediate one.
}
【问题讨论】:
-
你认为你可以将它重写为我们可以在嵌入中运行的东西吗?
-
为什么这是一个异步函数?看起来它唯一的异步部分是递归函数本身。