A code snippet that uses both Promise and Future
PS:the code is a part of AsyncController in the play-scala-starter-example
private def getFutureMessage(delayTime: FiniteDuration): Future[String] = { val promise: Promise[String] = Promise[String]() actorSystem.scheduler.scheduleOnce(delayTime) { promise.success("Hi!") }(actorSystem.dispatcher) // run scheduled tasks using the actor system's dispatcher promise.future }
What’s the difference between Futures and Promises
Aleksandar gave a great explanation here,to make it short.
1.You can think of futures and promises as two different sides of a pipe. On the promise side, data is pushed in, and on the future side, data can be pulled out.
2.To divide promises and futures into 2 separate interfaces was a design decision
3.By returning a promise you are giving the right to complete it to somebody else, and by returning the future you are giving the right to subscribe to it.