背景
从下面的代码可以看出:
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!] }
引用 Promise
的变量正在引用一个特殊的 Promise
对象,其中包含状态 或结果您传递给 Promise
构造函数的函数。如果您随后设置:
foo1 = null;
foo2 = null;
foo3 = null;
您无法再访问此状态或结果。
问题
在上述情况下,Promise
是否会被垃圾回收?如果没有,是否会产生导致内存泄漏的风险?
最佳答案
Does a
Promise
get garbage-collected in the above situation?
是的。在这方面,promise 对象与其他所有对象一样。
一些实现 (Firefox) 确实有特殊的行为,其中未处理的拒绝检测取决于垃圾收集,但这并没有真正改变正在收集的 promise 对象的任何内容。
关于javascript - 当您不再持有对 ES6-Promise 的引用时,ES6-Promise 会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638344/