I would like something like runProgram2 but currently that part does not compile. Is there a way to write it somewhat like runProgram2 but so it compiles..
package transformer
import scala.concurrent.{ExecutionContext, Promise, Future}
import ExecutionContext.Implicits.global
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
object TestingForComprehensions2 {
def main(args: Array[String]) = {
val future1: Future[String] = runMyProgram()
future1.onSuccess {
case r:String => System.out.println("result="+r)
}
val future2: Future[String] = runMyProgram2()
future2.onSuccess {
case r:String => System.out.println("result="+r)
}
System.out.println("waiting")
Thread.sleep(600000)
}
def runMyProgram() : Future[String] = {
val future = serviceCall()
val middle = serviceCallWrap(future)
val future2 = middle.flatMap(serviceCall2)
val future3 = future2.map(processAllReturnCodes)
future3
}
def runMyProgram2() : Future[String] = {
for {
result1 <- serviceCall()
middle = serviceCallWrap(result1)
result2 <- serviceCall2(middle)
} yield processAllReturnCodes(result2)
}
def processAllReturnCodes(theMsg: String) : String = {
"dean"+theMsg
}
def serviceCall() : Future[Int] = {
val promise = Promise.successful(5)
promise.future
}
def serviceCallWrap(f:Future[Int]) : Future[Int] = {
f
}
def serviceCall2(count:Int) : Future[String] = {
val promise = Promise.successful("hithere"+count)
promise.future
}
}
serviceCallWrapexpects a future, but you are passing anInt, first step would be to remove theresult1 <- serviceCall()part and call directlymiddle = serviceCallWrap(serviceCall()).Now
middleis a future butserviceCall2takes anInt, in this case you can extract the value from the future usingmiddle <- serviceCallWrap(serviceCall()), all together:If you want to keep the comprehension structure:
The only thing you can't keep (AFAIK) is the assignment expression for
middle, if you want to keep that you need to changeserviceCall2signature. This approach though has exactly the same result as the first approach, it's only more verbose.