使用Promise而不拒绝它会导致内存泄漏吗? [英] Does using Promise and not rejecting it cause memory leak?

查看:322
本文介绍了使用Promise而不拒绝它会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

function test(value){
  return new Promise(function (fulfill, reject){
     try {
       fulfill(true);
     } catch(e) {
       throw e;
     }
  });
}

我担心的是,当您使用Promisethrow error而不是reject(e)时,这会导致内存泄漏吗?

因为对我来说,抛出错误而不是拒绝错误不会拒绝或退出promise之外的错误.错误只会在Promise内部出现.让我知道您的意见.

解决方案

抛出错误将自动拒绝Promise.在此处

了解更多信息

但是有一些要讨论的东西.看下面的代码.代码抛出错误.错误从promise内部抛出.它将自动拒绝并启动捕获链.

function test(value){
  return new Promise(function (fulfill, reject){

       throw e;

  });
}

test('sample text').then(result=>console.log(result)).catch(result=>console.log(result))

但是,如果我在诺言中使用了Web API,例如setTimeout(),该怎么办.看下面的代码:

function test(value){
  return new Promise(function (fulfill, reject){
       setTimeout(function(){
          throw new Error('haha');
       },1000)


  });
}

test('sample text').then(result=>console.log(result)).catch(result=>console.log(result))

Web API是异步的.每当从promise内部调用Web API时,JavaScript引擎都会将async代码从外部执行.简单来说,Web API或异步代码将在主调用堆栈之外执行.

因此,从setTimeout()中引发错误将不会引用任何调用方promise,因此无法启动catch块.如果有任何错误,则需要从setTimeout()中将其从setTimeout()reject()启动catch块.

会导致内存泄漏吗?

答案: no

test().then().catch() 将在执行完毕后立即被垃圾回收.但是,如果将诺言保留在 var p = test(); p.then().catch() 这样的全局变量中,则变量 p 将保留在内存中,不会被垃圾回收.但这不是内存泄漏.内存泄漏是一个完全不同的方面,不适用于这种情况.

The code goes like this:

function test(value){
  return new Promise(function (fulfill, reject){
     try {
       fulfill(true);
     } catch(e) {
       throw e;
     }
  });
}

My concern is, when you use Promise and throw error instead of reject(e), will this cause a memory leak?

Because for me, throwing an error instead of rejecting it will not reject or exit the error outside promise. The error will just go around inside the Promise. Let me know your opinion.

解决方案

Throwing an error will automatically reject the Promise. Read more about it here

But there is something to discuss about. Look at the following code. The code throw an error.the error is thrown from the inside of a promise. It will automatically rejected and initiate the catch chain.

function test(value){
  return new Promise(function (fulfill, reject){

       throw e;

  });
}

test('sample text').then(result=>console.log(result)).catch(result=>console.log(result))

But what if I've used a Web API e.g setTimeout() inside my promise. Look at the following code:

function test(value){
  return new Promise(function (fulfill, reject){
       setTimeout(function(){
          throw new Error('haha');
       },1000)


  });
}

test('sample text').then(result=>console.log(result)).catch(result=>console.log(result))

Web APIs are asynchronous. Whenever a Web API is called from inside a promise, the JavaScript engine take that async code outside for execution. In simpler words, web APIs or asynchronous code gets executed outside of the main call stack.

So, throwing an error from setTimeout() won't have any reference of the caller promise, thus can't initiate the catch block. You need to reject() it from setTimeout() to initiate the catch block if there is any error.

Will it cause a memory leak?

Answer: no

the test().then().catch() will be garbage collected as soon as it finished executing. But if you would've kept the promise in a global variable like var p = test(); p.then().catch() then the variable p will stay in memory, it won't be garbage collected. But that's not a memory leak. Memory leak is a completely different aspect and doesn't apply in this kind of scenario.

这篇关于使用Promise而不拒绝它会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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