当你不再引用ES6-Promise时,它会发生什么? [英] What happens to an ES6-Promise when you no longer hold a reference to it?

查看:17
本文介绍了当你不再引用ES6-Promise时,它会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

您可以从以下代码中看到:

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会被垃圾回收吗?如果不会,是否不会产生导致内存泄漏的风险?

推荐答案

在上述情况下,Promise会被垃圾回收吗?

是的。在这方面,Promise对象就像其他所有对象一样。

某些实现(Firefox)确实有特殊的行为,其中未处理的拒绝检测依赖于垃圾回收,但这并不会真正改变正在收集的Promise对象的任何内容。

这篇关于当你不再引用ES6-Promise时,它会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆