r/javascript • • Oct 11 '17

JavaScript's Promise is Memory Unsafe

https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html
70 Upvotes

77 comments sorted by

View all comments

5

u/PM_YOUR_WORST_FEAR Oct 11 '17

Seems interesting, if you find yourself needing to use infinitely recursive promises.

Say you remove promises from this problem though:

function loop(n) {

const i = signal(n);

if (i % 1000 == 0) console.log(i);

return loop(n + 1);

}

Would you say JavaScript is doing something wrong if it "leaks" when you run that code?

What's the difference here?

5

u/alexelcu Oct 11 '17

The result is that the stack blows up, triggering a stack overflow error.

Yes, it's a mistake, explaining the reason for why is a little difficult, I tried doing that in a comment here.

But also consider that if this wouldn't be a mistake, then they wouldn't be implementing TCO in ECMAScript 6: http://2ality.com/2015/06/tail-call-optimization.html