Skip to content

Instantly share code, notes, and snippets.

@gkazior
Created October 8, 2015 10:24
Show Gist options
  • Save gkazior/a97b47dc5dc081e483a5 to your computer and use it in GitHub Desktop.
Save gkazior/a97b47dc5dc081e483a5 to your computer and use it in GitHub Desktop.
Futures samples
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
import scala.util._
val timeout = 1.seconds
//> timeout : scala.concurrent.duration.FiniteDuration = 1 second
case class LongComputation(i: Int)
def getSimpleComputation(i: Int, canFail: Boolean = true): Future[LongComputation] = {
//Future.successful(LongComputation(i))
Future {
if (canFail) {
if (Math.random() > 0.1) throw new RuntimeException(s"Long computation of $i has failed")
}
LongComputation(i)
}
} //> getSimpleComputation: (i: Int, canFail: Boolean)scala.concurrent.Future[com.
//| gk.test.FuturesBasicsWs.LongComputation]
def getComplexComputation(i: Int, j: Int): Future[LongComputation] = {
Future {
LongComputation(i * j)
}
} //> getComplexComputation: (i: Int, j: Int)scala.concurrent.Future[com.gk.test.F
//| uturesBasicsWs.LongComputation]
def getComposedComputation(f: (Int, Int) => Future[LongComputation], base: Future[LongComputation], when6: Future[LongComputation]): Future[LongComputation] = {
val p = Promise[LongComputation]()
Future {
base onFailure {
case e =>
println(s"base failure with\n ${e.getMessage}")
p.failure(e)
}
base onSuccess {
case LongComputation(6) =>
println("base success with 6")
p.completeWith(when6)
case LongComputation(base) =>
println(s"base success with $base")
f(2, base) onComplete {
case Success(LongComputation(v)) =>
println(s"f(2,$base)=$v")
p.success(LongComputation(v + base))
case Failure(e) =>
println(s"f(2,$base) failure")
p.failure(e)
}
}
}
p.future
} //> getComposedComputation: (f: (Int, Int) => scala.concurrent.Future[com.gk.te
//| st.FuturesBasicsWs.LongComputation], base: scala.concurrent.Future[com.gk.t
//| est.FuturesBasicsWs.LongComputation], when6: scala.concurrent.Future[com.gk
//| .test.FuturesBasicsWs.LongComputation])scala.concurrent.Future[com.gk.test.
//| FuturesBasicsWs.LongComputation]
val cf = getComposedComputation(getComplexComputation, getSimpleComputation(61), getSimpleComputation(911))
//> cf : scala.concurrent.Future[com.gk.test.FuturesBasicsWs.LongComputation]
//| = scala.concurrent.impl.Promise$DefaultPromise@6cf5b81a
val cfRecovered = cf.recoverWith {
case ex: Exception =>
println(s"Well the future failed with:\n [${ex.getMessage}],\n but we want a value in that case!")
Future.successful(LongComputation(-1))
} //> base failure with
//| Long computation of 61 has failed
//| cfRecovered : scala.concurrent.Future[com.gk.test.FuturesBasicsWs.LongComp
//| utation] = scala.concurrent.impl.Promise$DefaultPromise@448e7f2f
Await.result(cfRecovered, timeout) //> Well the future failed with:
//| [Long computation of 61 has failed],
//| but we want a value in that case!
//| res0: com.gk.test.FuturesBasicsWs.LongComputation = LongComputation(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment