final case class Await[Value](future: Future[Value]) extends AnyVal with Keyword[Await[Value], Value] with Product with Serializable
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)
- Source
- Await.scala
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)
, !Await
can be used together withtry
/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!"))
, 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!"))
- Alphabetic
- By Inheritance
- Await
- Serializable
- Serializable
- Product
- Equals
- Keyword
- AnyVal
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (Await[Value], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
apply[Domain](handler: (Value) ⇒ Domain)(implicit dsl: Dsl[Await[Value], Domain, Value]): Domain
An alias to cpsApply.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
cpsApply[Domain](handler: (Value) ⇒ Domain)(implicit dsl: Dsl[Await[Value], Domain, Value]): Domain
- Definition Classes
- Keyword
- Annotations
- @inline()
- def ensuring(cond: (Await[Value]) ⇒ Boolean, msg: ⇒ Any): Await[Value]
- def ensuring(cond: (Await[Value]) ⇒ Boolean): Await[Value]
- def ensuring(cond: Boolean, msg: ⇒ Any): Await[Value]
- def ensuring(cond: Boolean): Await[Value]
- def formatted(fmtstr: String): String
- val future: Future[Value]
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def unary_!: Value
- def →[B](y: B): (Await[Value], B)