在给定的函数
async function x(n) {
console.log(n);
console.trace();
if (n >= 3) { return; };
await setTimeout(() => x(n+1), 1000);
}
x(0);
我可以看到控制台中的痕迹越来越长.
将其与递归版本进行比较,
async function x(n) {
console.log(n);
console.trace();
if (n >= 3) { return; };
x(n+1);
}
x(0);
你可以看到痕迹也在增长.那么,setTimeout是否也在增加堆栈,或者这种增长跟踪背后的解释是什么?