com.thoughtworks.dsl.keywords
Await is a Keyword to extract value from a scala.concurrent.Future.
This keyword is available in functions whose return types are Future, domains.task.Task, or any exception aware continuations as (_ !! Throwable !! _).
(_ !! Throwable !! _)
杨博 (Yang Bo)
Other keywords, including Return or Get, can be used together with Await
import scala.concurrent.Future import com.thoughtworks.dsl.keywords.{Get, Return} val buffer = new StringBuffer def recoverFuture = Future { buffer.append("Oh") } def exceptionalFuture = Future[StringBuffer] { throw new IllegalStateException("No") } def myFuture: Char => Future[StringBuffer] = !Return { try { !Await(exceptionalFuture) } catch { case e: IllegalStateException => !Await(recoverFuture) buffer.append(!Get[Char]) buffer.append(e.getMessage) } finally { buffer.append("!") } } myFuture(' ').map(_.toString should be("Oh No!"))
!Await can be used together with try / catch / finally.
!Await
try
catch
finally
import scala.concurrent.Future val buffer = new StringBuffer def recoverFuture = Future { buffer.append("Oh") } def exceptionalFuture = Future[StringBuffer] { throw new IllegalStateException("No") } def myFuture = Future { try { !Await(exceptionalFuture) } catch { case e: IllegalStateException => !Await(recoverFuture) buffer.append(' ') buffer.append(e.getMessage) } finally { buffer.append("!") } } myFuture.map(_.toString should be("Oh No!"))
Given a Future:
import scala.concurrent.Future val myFuture40 = Future { 40 }
You can Await the Future in another Future
def myFuture42 = Future { !Await(myFuture40) + 2 }
A Future can be converted to a domains.task.Task with the help of Await.
import com.thoughtworks.dsl.domains.task.Task import com.thoughtworks.dsl.keywords.Await val myTask = Task { !Await(myFuture42) }
Then a domains.task.Task can be converted back to a scala.concurrent.Future via domains.task.Task.toFuture.
val myAssertionTask = Task { !Shift(myTask) should be(42) } Task.toFuture(myAssertionTask)
An alias to cpsApply.
Await is a Keyword to extract value from a scala.concurrent.Future.
This keyword is available in functions whose return types are Future, domains.task.Task, or any exception aware continuations as
(_ !! Throwable !! _)
.Author:
杨博 (Yang Bo)
Other keywords, including Return or Get, can be used together with Await
!Await
can be used together withtry
/catch
/finally
.Given a Future:
You can Await the Future in another Future
A Future can be converted to a domains.task.Task with the help of Await.
Then a domains.task.Task can be converted back to a scala.concurrent.Future via domains.task.Task.toFuture.