Just reading this article on promises, and the author uses the following example:
def redeemCampaignPledge(): Future[TaxCut] = {
val p = Promise[TaxCut]()
Future {
println("Starting the new legislative period.")
Thread.sleep(2000)
//p.success(TaxCut(20))
//println("We reduced the taxes! You must reelect us 2018!")
p.failure(LameExcuse("global economy crisis"))
println("We didn't fullfil our promises, so what?")
}
p.future
}
val taxCutF: Future[TaxCut] = redeemCampaignPledge()
println("Now they're elected, let's see if they remember their promise.")
taxCutF.onComplete {
case Success(TaxCut(reduction)) =>
println(s"Miracle! Taxes cut by $reduction percent.")
case Failure(ex) =>
println(s"They broke the promise again. Because of a ${ex.getMessage}")
}
My question is, can't I just get rid of Promises and rewrite it as:
def redeem(): Future[TaxCut] = Future {
println("Starting legislative period...!!!!")
Thread.sleep(2000)
println("We were successful")
TaxCut(25)
}
What is this second version lacking? I am not fully grasping the value that promises bring.