【发布时间】:2018-08-16 09:00:13
【问题描述】:
我正在使用 Node v8.10.0
上述问题解释了 Node.js 如何不再支持 TCO。不过,我最近遇到了这样一个函数的问题:
async function processBatch(nthBatch) {
// do a bunch of async work and build up variables
await processBatch(nthBatch + 1);
}
代码有内存泄漏,通过将其更改为立即修复:
async function processBatch(nthBatch) {
// do a bunch of async work and build up variables
return processBatch(nthBatch + 1);
}
我很惊讶它确实有效,因为在上述问题中,它清楚地说明了 Node 8.x 不支持 TCO。那么这里有什么特别的事情可以实现 TCO 吗?还是因为它在后台使用了一个生成器,而 return 将生成器标记为完成,所以可以丢弃堆栈?
【问题讨论】:
标签: node.js recursion tail-recursion