Imagine I am running a lengthy operation for a series of values.
The function responsible for starting this operation is startNext()
and the final line executed in it is a recursive call like so:
function startNext(){
var val = getNextValue()
workOnValue(val)
.then(doSomeMoreWork)
.then(doMoreStuff)
.then(moree)
.then(startNext);
}
Since tail recursion does not currently work in JS, the stack will continue to grow. Would changing the last line to:
.then(function(){setTimeout(startNext, 0)});
be more effective? This approach may prevent the stack from filling up since it adds a new operation to the event loop.