object Async
Async blocks provide a direct means to work with scala.concurrent.Future.
For example, to use an API that fetches a web page to fetch two pages and add their lengths:
import ExecutionContext.Implicits.global import scala.async.Async.{async, await} def fetchURL(url: URL): Future[String] = ... val sumLengths: Future[Int] = async { val body1 = fetchURL("http://scala-lang.org") val body2 = fetchURL("http://docs.scala-lang.org") await(body1).length + await(body2).length }
Note that in the following program, the second fetch does *not* start
until after the first. If you need to start tasks in parallel, you must do
so before await
-ing a result.
val sumLengths: Future[Int] = async { await(fetchURL("http://scala-lang.org")).length + await(fetchURL("http://docs.scala-lang.org")).length }
- Alphabetic
- By Inheritance
- Async
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- macro def async[T](body: => T)(implicit execContext: ExecutionContext): Future[T]
Run the block of code
body
asynchronously.Run the block of code
body
asynchronously.body
may contain calls toawait
when the results of aFuture
are needed; this is translated into non-blocking code. - def asyncImpl[T](c: Context)(body: reflect.macros.whitebox.Context.Tree)(execContext: reflect.macros.whitebox.Context.Tree)(implicit arg0: reflect.macros.whitebox.Context.WeakTypeTag[T]): reflect.macros.whitebox.Context.Tree
- def await[T](awaitable: Future[T]): T
Non-blocking await the on result of
awaitable
.Non-blocking await the on result of
awaitable
. This may only be used directly within an enclosingasync
block.Internally, this will register the remainder of the code in enclosing
async
block as a callback in theonComplete
handler ofawaitable
, and will *not* block a thread.- Annotations
- @compileTimeOnly("[async] `await` must be enclosed in an `async` block")
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()