--

"The fact is that the above code won't unsubscribe and generate a memory leak, because at the moment the returned function is generated, unsubscribe still has () => undefined value."

It is wrong.

You don't need an object nor a reference.

The moment the function is generated doesn't matter. What matters is the moment the return clean-up function gets run.

At that moment unsubscribe variable can point the null function or the correct unsubscribe function, depending if the cleanup code is run before or after the await someAyncCode();

The problem of running the clean-up code before someAsyncCode is not resolved in your examples.

Perhaps you should refresh how closures works?

--

--

Andrea Koutifaris
Andrea Koutifaris

Responses (2)