0

I'm trying to achieve a pattern in which I invoke an endless recursive loop of thenables (Promise-based chaining) that are likely to run for several hours (I'm playing around with a little lovefield project).

Basically what I'm trying is this:

runMyRoutine() {
    return doThis().then(function() {
        return doThat().then(function() {
            return runMyRoutine();
        });
    });
}

Is there some best practice available on this issue (some kind of guideline or a special API to use in that case?)

In terms of feasability it should be possible in my opinion to just keep the above structure, but aren't there memory concerns?

Best regards, lt1

ltetzlaff
  • 11
  • 1
  • 5

1 Answers1

0

You can't get away with truly endless recursion. You'll get a stack overflow at some point.

function runMyRoutine() {
  runMyRoutine();  // call stack overflows here at some point
}

However, you don't actually have endless recursion in your case. If doThis() is set up correctly, the last runMyRoutine() is effectively on its own call stack (the same stack with whatever ends up calling the function wrapping it). It's disconnected from the original context.

Brad
  • 159,648
  • 54
  • 349
  • 530