最佳答案
我正在阅读 SIP-14和 Future
的概念是完美的意义和容易理解。但是对于 Promise
有两个问题:
这怎么可能? Future
和 Promise
不是两种不同的类型吗?
我们应该什么时候使用 Promise
? 示例 producer and consumer
代码:
import scala.concurrent.{ future, promise }
val p = promise[T]
val f = p.future
val producer = future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
}
val consumer = future {
startDoingSomething()
f onSuccess {
case r => doSomethingWithResult()
}
}
is easy to read but do we really need to write like that? I tried to implement it only with Future and without Promise like this:
val f = future {
produceSomething()
}
val producer = future {
continueDoingSomethingUnrelated()
}
startDoingSomething()
val consumer = future {
f onSuccess {
case r => doSomethingWithResult()
}
}
这个例子和给定的例子有什么区别? 是什么使承诺成为必要?