Another slant on this is that if the promise executor function, inlined as
resolve => {
// nothing doing with the resolve
}
doesn't record the value of resolve
for later use, or create callbacks in the executor function that call the executor's resolve
argument, then the executor function can't reach resolve
in any of its content.
However, resolve
and reject
functions hold the promise they were issued for in memory since they themselves have internal access to the promise. If resolve
and/or reject
are not reachable in code, neither has been called, any externally stored reference(s) to the promise are not reachable either, and the promise has not been used to resolve another promise, the promise becomes eligible for memory garbage collection.
The posted code contains no code referencing resolve
, and the external reference to the promise, myPromise
, becomes unreachable after the second statement using it has been executed.
When myPromise
is gargage collected, its references to resolve and reject functions of the next promise in the chain (the one returned from then
) are released and so the next promise can be garbage collected, which in turn will release resolve/reject refererences to the one returned from catch
.
At this point all program data has or can be garbage collected, and in short the program has run to completion.
await
myPromise. – Hardnettresolve => {}
in your example is a function that doesn't start any new tasks. If there were asetTimeout
orfetch
, there would be a pending task. If you were to callresolve()
there would be one, too. But since there are no tasks to run, it's' free to exit. – Ignominyconsole.log('result')
onceresolve()
is called from inside ofnew Promise(resolve => {})
. Learn more about Promises on MDN – Carte