Background
You can see from the following code:
var foo1 = new Promise (function (resolve, reject){};
var foo2 = new Promise (function (resolve, reject) {
resolve('succes!');
});
var foo3 = new Promise (function (resolve, reject) {
reject(Error('Failure!'));
});
console.log (typeof foo1 === 'object'); // true
console.log (Object.getOwnPropertyNames(foo1)); // []
console.log (foo1.length); // undefined
console.log (foo1); // Promise { <pending> }
console.log (foo2); // Promise { 'succes!' }
console.log (foo3); // Promise { <rejected> [Error: Failure!] }
that the variable referencing a Promise
is referencing a special Promise
object containing either the state or the outcome of the function you pass to the Promise
constructor. If you then set:
foo1 = null;
foo2 = null;
foo3 = null;
you are no longer able to access this state or outcome.
Question
Does a Promise
get garbage-collected in the above situation and if no, does that not create a risk of causing memory leaks?
Does a
Promise
get garbage-collected in the above situation?
Yes. A promise object is just like every other object in that regard.
Some implementations (Firefox) do have special behaviour where unhandled-rejection detection depends on garbage collection, but that doesn't really change anything about the promise object being collected.