As it was discussed earlier, JavaScript promises have nothing special with respect to garbage collecting. In the other hand, both async functions and await operations are wrappers around promises. So I wonder whether it is possible for the promise returned from an async function be GCed if no one holds a reference to it and internal awaiting takes too much time?
function getDelayedPromise() {
return new Promise((resolve) => setTimeout(resolve, someBigNumber));
}
async function bar(data) {
await getDelayedPromise(data);
return data;
}
function DOMEventHandler() {
bar().then(updateUI);
}
(Let's suppose DOMEventHandler
sunchronously gets runned to handle some DOM event)
Under the hood, await
fulfills then()
on the delayed promise to continue function execution after the promise be settled. However, the promise associated with an async function itself (one returned from the bar()
) is not placed in the memory in this way. In fact, the spec prescribes to it wait for async function completion to be settled with its value thereafter:
a. Let result be the result of evaluating asyncBody.
...
d. If result.[[Type]] is normal, then
i. Perform ! Call(promiseCapability.[[Resolve]], undefined, « undefined »).
...
(where promiseCapability
represents an async function promise)
Is this waiting a guarantee of bar()
promise should not be collected before an async function body is evaluated? In other words, is there likelihood that updateUI()
will never be executed?