当我不使用“then”时,是否存在任何(负面)副作用?承诺的功能? [英] Are there any (negative) side effects when I don't use the "then" function of a Promise?

查看:91
本文介绍了当我不使用“then”时,是否存在任何(负面)副作用?承诺的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回Promise的函数。

I have a function which returns a Promise.

现在,有时消费者在Promise上使用then函数是有意义的。但有时消费者根本不关心Promise什么时候解决,也不关心结果 - 换句话说,同样的功能也应该能够以一劳永逸的方式被调用。

Now, sometimes it makes sense for the consumer to use the "then" function on that Promise. But sometimes the consumer simply does not care about when the Promise resolves, and also not about the result - in other words the same function should also be able to be called in a "fire and forget" manner.

所以我想要这两种使用场景:

So I want these two usage scenarios:

func().then( ... ); // process Promise
func(); // "fire and forget"

这显然有效,但我想知道这是否被认为是不良做法 ,特别是如果这种使用模式可能有任何不必要的副作用,即。导致内存泄漏?现在我正在使用蓝鸟,但我认为如果有任何不同,可以切换到原生Promise。

This apparently works, but I wonder if this is considered "bad practice", and in particular if this usage pattern could have any unwanted side effects, ie. leading to memory leaks? Right now I am using bluebird, but I consider to switch to native Promises if that makes any difference.

推荐答案

记住每次通话到然后会产生新的承诺。因此,任何由于承诺没有附加任何解析处理程序而导致内存泄漏的Promise实现将是一个破坏的实现,所有这些承诺,我们永远不会挂钩处理程序返回。我非常怀疑ES2015承诺的实现,Bluebird,Q等都有这种行为。

Remember that every call to then results in a new promise. So any Promise implementation that had a memory leak as a result of a promise not having any resolution handlers attached would be a broken implementation, what with all those promises that we never hook handlers to being returned. I very much doubt implementations of ES2015's promises, Bluebird, Q, etc. have that behavior.

另外,从概念上讲,promise的解析处理程序基本上只是承诺存储的函数然后在适当时调用,如果你从未给它存储任何函数,它就不会是内存泄漏。

Separately, as conceptually a promise's resolution handlers are basically just functions stored by the promise and then called when appropriate, it's not likely to be a memory leak if you never give it any functions to store.

但是,有你的即发即忘的问题,只是没有内存泄漏问题:它违反了一个主要的Promise规则:要么处理拒绝,要么将promise链返回到其他可以处理拒绝的东西。由于您没有这样做,如果操作失败,您将遭到未处理的拒绝。未处理的拒绝被报告给控制台,并且在某些环境中可能会终止您的应用程序(在某些时候,Node.js可能会在发生这种情况时开始终止进程​​,请参阅这个未解决的问题)。

But, there is a problem with your fire-and-forget, just not a memory leak problem: It breaks one of the main Promise rules: Either handle rejection, or return the promise chain to something else that will handle rejection. Since you're not doing that, if the operation fails, you'll have an unhandled rejection. Unhandled rejections are reported to the console and in some environments may terminate your app (at some point, Node.js may start terminating the process when this happens, see this open issue).

如果那么返回一个新的承诺是一个惊喜,考虑:

If the fact that then returns a new promise is a surprise, consider:

let p1 = new Promise(resolve => {
  setTimeout(() => {
    resolve('a');
  }, 100);
});
let p2 = p1.then(result => {
  console.log("p1.then got " + result);
  return 'b';
});
p2.then(result => {
  console.log("p2.then got " + result);
});

输出


p1.then got a
p2.then got b

这篇关于当我不使用“then”时,是否存在任何(负面)副作用?承诺的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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