Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

63 lines (46 sloc) 1.69 KB


Dispatch is a library for asynchronous HTTP interaction. It provides a Scala vocabulary for Java's async-http-client. The latest release version is 0.9.0. For information on Dispatch 0.8.x, see dispatch-classic.

Diving in

import dispatch._
val doc = Http(host("") OK as.String)

The above defines a request from the given host where 2xx responses are handled as a string. Since Dispatch is fully asynchronous, doc represents a promise of the string rather than the string itself.

Deferring action

You can act on the response once it's available with a for-expression.

for (docstr <- doc)

This for-expression applies to any successful response that is eventually produced. If no successful response is produced, nothing is printed. This is how for-expressions work in general. Consider a more familiar example:

val opt: Option[String] = None
for (optstr <- opt)

An option may or may not contain a value, just like a promise may or may not produce a successful response. But while any given option already knows what it is, a promise may not. So the promise behaves asynchronously in for-expressions, to avoid holding up operations subsequent that do not depend on its value.

Demanding answers

As with options, you can assert that a promised value be available at any time with the use of apply:

val docstr = doc()

But the wise use of promises defers this operation as long as is practical, or doesn't perform it at all. To see how, keep reading.

Jump to Line
Something went wrong with that request. Please try again.