> For the complete documentation index, see [llms.txt](https://devon-miller.gitbook.io/my-dispatch-user-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devon-miller.gitbook.io/my-dispatch-user-notes/composing_results/scala_async_macro.md).

# Scala Async Macro

Scala has an async macro that manages your `Future`s and `Await`s to compose asynchronous results.

The basic idea is to do something like this (stolen from [here](https://github.com/scala/async)):

```scala
import ExecutionContext.Implicits.global
import scala.async.Async.{async, await}

val future = async {
  val f1 = async { ...; true }
  val f2 = async { ...; 42 }
  if (await(f1)) await(f2) else 0
}
```

or some variation of this pattern. Depending on how you you structure your calls to await or when you create (and start) the future, you may get parallelism or just non-blocking.

Since nearly all remote calls should be considered "slow", the other example on the site where I found the first example above is:

```scala
def combined: Future[Int] = async {
  val future1 = slowCalcFuture
  val future2 = slowCalcFuture
  await(future1) + await(future2)
}
```

There is not more more to say here. Since dispatch returns a future, just use the async macro to compose your futures as needed for your application.

The dispatch documentation discusses for comprehensions for composing your futures, and the async macro is equivalent to that but with nicer and clear syntax, at least in my opinion.

As final proof that this works, you can add a route to our example server that delays a specific amount of time then returns the delay parameter that is provided in the URL of the GET request:

```scala
 ~ path("delay" / LongNumber) { n =>
        withoutRequestTimeout {
          get {
            import scala.concurrent._
            import scala.concurrent.duration._
            println(s"Delaying $n milliseconds.")           
            complete(akka.pattern.after(n.millis, system.scheduler)(Future { n.toString }))
          }
        }
      }
```

By calling `localhost:9000/delay/10000`, you can delay the response by 10 seconds and the number 10000 is returned in the body. Hence, in amm we could do something like:

```scala
load.ivy("org.scala-lang.modules" %% "scala-async" % "latest.release")
load.ivy("net.databinder.dispatch" %% "dispatch-core" % "0.11.3")

import scala.async.Async._
import dispatch._, Defaults._
import scala.concurrent.Await
import scala.concurrent.duration._

def callit(n: Long) =
  Http(url(s"http://localhost:9000/delay/$n") OK as.String).map(_.toLong)

val d1 = 5000
val d2 = 2000

println(s"Delay 1 is $d1, Delay 2 is $d2")

val resultFuture = async {
  val future1 = callit(d1)
  val future2 = callit(d2)
  await(future1) + await(future2)
}
resultFuture onComplete { r => println(s"Result $r")}
```

and you get a result of

```
Result Success(7000)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devon-miller.gitbook.io/my-dispatch-user-notes/composing_results/scala_async_macro.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
