为什么我们既需要未来又需要承诺? [英] Why do we need both Future and Promise?
问题描述
据我所知,Future
是只读的,而Promise
是一次写入的数据结构.
As I know, Future
is read-only and Promise
is write-once data structure.
我们需要Promise
来完成Future
例如
object Lie extends Throwable
val lie = Future { throw Lie }
val guess = Promise[String]()
lie.onComplete { case Success(s) => guess.success("I knew it was true!")
case Failure(t) => guess.failure("I knew it was lie")}
// return type: Unit
guess.future.map(println)
// res12: scala.concurrent.Future[Unit] = List()
// I knew it was lie!
// Requires Promise to chain Future with exception
但是,我不明白为什么我们需要同时拥有Future
和Promise
But, I can't understand why we need to have both Future
and Promise
我猜因为Future.onComplete
签名需要Promise
由于Future.onComplete
返回类型为Unit
,因此Future
(可能存在例外)无法链接
Since Future.onComplete
return type is Unit
,Future
with possible exceptions cannot be chained
我认为引入Promise
是为了克服这一局限性
I assume Promise
was introduced to overcome this limitation
但是为什么不只更改Future.onComplete
的签名呢?
But why not just change the signature of Future.onComplete
?
将Future.onComplete
的返回类型更改为Future[T]
将启用Future
上的链接,但有例外
Changing the return type of Future.onComplete
as Future[T]
will enable chaining on Future
with exception
,然后Future
不需要Promise
例如,上面的代码可以更改为
For example, code above can be changed into
val lie = Future { throw Lie }
lie.onComplete {
case Success(s) => "I knew it was true!"
case Failure(t) => "I knew it was lie!"
}.map(println)
//onComplete return type is Future[String]
我的问题是
1)我是对的吗?如果将onComplete签名从Unit
更改为Future[T]
,Future
是否不需要Promise
?
1) am I right? does Future
not need Promise
, If onComplete signature is changed from Unit
to Future[T]
?
2)为什么首先将Future和Promise分开?
2) Why Future and Promise are divided in the first place ?
UDPATE
感谢回复者,我现在了解Promise的目的.实际上不是Future
链接
Thanks to the repliers, Now I understand the purpose of Promise. It wasn't actually for Future
chaining
如果可以的话,我可以问你吗
If I may, can I ask you
为什么onComplete
返回Unit
??
它实际上可以返回Future[T]
以便轻松地链接Future
It can actually return Future[T]
to enable chaining Future easily
例如
Future { throw Error }.onComplete {
case Success(s) => "Success"
case Failure(t) => throw Error
}.onComplete {
case Success(s) => "Success"
case Failure(t) => throw Error
}. ...
推荐答案
我是对的吗?
Future
不需要Promise
,如果onComplete
签名是 从单位改为未来[T]?
am I right? does
Future
not needPromise
, IfonComplete
signature is changed from Unit to Future[T]?
您正在将事情混为一谈.让我们澄清一下.
You're mixing things up a little. Let's clarify.
A Future[T]
表示将来会完成的计算.也就是说,您传递Future.apply
一个函数,该函数将在您定义的某些ExecutionContext
分配的线程上执行.
A Future[T]
represents a computation which will complete in the future. That is, you pass Future.apply
a function which will execute on a thread assigned by some ExecutionContext
you define.
另一方面,现在Promise[T]
是创建Future[T]
的一种方式,而无需实际创建Future[T]
.一个很好的例子是Future.successful
方法(它将在内部消耗Promise.successful
):
Now, on the other hand, a Promise[T]
is a way to create a Future[T]
, without actually creating a Future[T]
. A good example for this would be the Future.successful
method (which will internally consume Promise.successful
):
def successful[T](result: T): Future[T] = Promise.successful(result).future
这不需要ExecutionContext
,也不需要排队,如果有任何其他资源.它只是一个便利包装,它使您可以人为"创建Future[T]
.
This requires no ExecutionContext
and no queuing if any additional resources. It's merely a convenience wrapper that allows you to "artificially" create a Future[T]
.
这篇关于为什么我们既需要未来又需要承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!