I like JavaScript so far, and decided to use Node.js as my engine partly because of this, which claims that Node.js offers TCO. However, when I try to run this (obviously tail-calling) code with Node.js, it causes a stack overflow:
function foo(x) {
if (x == 1) {
return 1;
}
else {
return foo(x-1);
}
}
foo(100000);
Now, I did some digging, and I found this. Here, it seems to say I should write it like this:
function* foo(x) {
if (x == 1) {
return 1;
}
else {
yield foo(x-1);
}
}
foo(100000);
However, this gives me syntax errors. I've tried various permutations of it, but in all cases, Node.js seems unhappy with something.
Essentially, I'd like to know the following:
- Does or doesn't Node.js do TCO?
- How does this magical
yield
thing work in Node.js?