Why is this not a memory leak?

I am interested in using the generator template + promise, where the promise will go through the generator, resolve each received value and pass it back to the generator. Once this is done, the promise will be eliminated with a final refund (if any).

I found the elegant implementation provided by promjs.org at the bottom of this page , but having studied it a bit, it seems to me that this will be a memory leak for long or infinite generators. I hope they fix you.

I simplified this by removing error checking and using arrow functions to read:

function async(generator){
  function handle(result) {
    return result.done
      ? Promise.resolve(result.value)
      : Promise.resolve(result.value)
               .then(res => handle(generator.next(res)));
  }

  return handle(generator.next());
}

It seems to me that each iteration handlereturns a new promise that will not be resolved until the generator completes, and then all of them are cascaded. This is so, and if so, then it will crash with enough iterations?

+4
source share

Source: https://habr.com/ru/post/1689917/


All Articles