I have two recursive promise chains that seem to be doing the same thing, but one of them is causing a memory leak while the other is not. I’ve simplified the code to the following examples:
Leaking code:
function leakingLoop() {
return delay(1)
.then(() => {
console.log(`Tick ${Date.now()}`)
return leakingLoop()
})
}
Non-leaking code:
function nonLeakingLoop() {
delay(1)
.then(() => {
console.log(`Tick ${Date.now()}`)
nonLeakingLoop()
})
}
The only difference between the two is that the leakingLoop function returns the result of the delay promise, while the nonLeakingLoop function does not.
Can someone explain why the leakingLoop function is causing a memory leak, while the nonLeakingLoop function is not? Is there something about the way promises are handled that I’m missing?
Note: I’ve assumed that the delay function is a promise-based function that resolves after a certain amount of time. If that’s not the case, please let me know and I’ll update the question accordingly.