Getting Started with Async
Welcome! This tutorial introduces Async[A], a zero-allocation effect type from ZIO Blocks that unifies ready values, failures, and genuinely suspended computations under a single type and combinator set. If you know basic Scala syntax and have a sense of what an effect type is, you have everything you need to follow along.
1. Introduction
By the end of this tutorial, you will be able to:
- Create ready-value effects with
Async.succeedand transform them withAsync#mapandAsync#block. - Handle errors with
Async.fail,Async#catchAll, andAsync#either. - Write sequential async code in direct style using
Async.async { … .await … }. - Bridge throw-based code and callback-based APIs with
Async.attemptandAsync.promise/Completer. - Fork background computations with
Async.startand cancel them withAsync.Running#cancel.
To add the async module to your project, include this dependency in your build.sbt:
libraryDependencies += "dev.zio" %%% "zio-blocks-async" % "0.0.56"
Then bring the full DSL into scope at the top of each file you use it from:
import zio.blocks.async._
We recommend reading from top to bottom — each section builds directly on the one before it.
2. Background: What Is Async[A]?
Async[A] was designed to solve a specific problem: code often juggles three different kinds of values — results that are already available, computations that need to wait for I/O or a callback, and failures. Treating these differently in different places creates friction. Async[A] is one abstraction that covers all three.
The design's most important property is its happy-path allocation budget: zero. When you chain Async.succeed, Async#map, and another map call together, every step is a plain function call. No wrapper objects accumulate on the heap. Only a computation that truly suspends — waiting for a callback, a timer, or a thread — leaves a pending object behind. This is the zero-allocation promise: you pay for suspension only when you actually suspend.
We will explore Async[A] through an order-processing scenario: looking up a user via a callback API, parsing an order ID, checking stock availability in the background, and recovering gracefully from any failure — one concept at a time.
3. Ready Values: Async.succeed, map, and block
The simplest async computation is one that already has its answer. Async.succeed(value) wraps an available value into an Async[A] so it can take part in async chains without allocating anything. Once you have an Async, you transform it with Async#map and drive it to its final result with Async#block.
Here we wrap the integer 42, double it with map, then extract the result:
import zio.blocks.async._
val result: Int = Async.succeed(42).map(_ * 2).block
println(s"Ready mapped: $result")
Expected output:
Ready mapped: 84
Async.succeed(42)wraps42as a ready-valueAsync[Int]with zero allocation.- Calling
mapwith_ * 2applies the function; because the input is already ready, the entire chain is just a function call — no suspension object is created. - The
blockcall is the eager driver — it polls the async until it completes and returns the final value. It is safe to call on the JVM; on Scala.js it throws if the computation is still suspended.
Try changing 42 to a different number and watch the output change accordingly.
Runnable version: Concept1ReadyValuesExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept1ReadyValuesExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept1ReadyValuesExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Ready Values: `Async.succeed`, `map`, and
* `block`
*
* Wraps an available value with `Async.succeed`, transforms it with
* `Async#map`, and drives it to its result with `Async#block`.
*/
object Concept1ReadyValuesExample {
def main(args: Array[String]): Unit = {
val result: Int = Async.succeed(42).map(_ * 2).block
println(s"Ready mapped: $result")
}
}
4. Error Handling: Async.fail, catchAll, and either
Not every computation succeeds. Async.fail(throwable) creates a failed async that short-circuits all downstream map calls without invoking their functions. Async#catchAll recovers by applying a function that returns a new Async.
Here we create a failed computation and recover from it with catchAll:
import zio.blocks.async._
val recovered: String = Async
.fail(new Exception("oops"))
.catchAll(_ => Async.succeed("default"))
.block
println(s"Recovered: $recovered")
Expected output:
Recovered: default
Async.fail(new Exception("oops"))creates a computation that carries the exception as its failure.- Calling
catchAllwith a recovery function intercepts the failure; the lambda ignores the specific error and returns a ready-value replacement. - The
blockcall drives the recovered chain to its result,"default".
Sometimes you want to observe both the success and failure branches as plain data rather than handle them immediately. Async#either reifies both outcomes as Either[Throwable, A] so you can pattern-match on them:
import zio.blocks.async._
val observed: Either[Throwable, Int] = Async
.fail(new Exception("error"))
.either
.block
println(s"Observed: $observed")
Expected output:
Observed: Left(java.lang.Exception: error)
- Calling
eitherwraps the failure inLeft; a successful result would appear inRight, turning the async's outcome into an ordinary Scala value. - The
blockcall materialises thatEitherso the learner can inspect it.
Runnable version, with both blocks of this section: Concept2ErrorHandlingExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept2ErrorHandlingExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept2ErrorHandlingExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Error Handling: `Async.fail`, `catchAll`, and
* `either`
*
* Recovers a failed `Async` with `Async#catchAll`, then observes a failure as
* data with `Async#either`.
*/
object Concept2ErrorHandlingExample {
def main(args: Array[String]): Unit = {
val recovered: String = Async
.fail(new Exception("oops"))
.catchAll(_ => Async.succeed("default"))
.block
println(s"Recovered: $recovered")
val observed: Either[Throwable, Int] = Async
.fail(new Exception("error"))
.either
.block
println(s"Observed: $observed")
}
}
5. Direct Style: Async.async and await
Writing nested Async#flatMap chains is precise but becomes hard to read when many steps depend on each other. Async.async { … } lets you write that same sequencing in direct style: inside the block, call Async#await on any Async to extract its value and bind it to a local variable, as if you were writing straight-line code. The compiler rewrites every await call into a flatMap chain at compile time, so the runtime behaviour is identical.
Here we compose a user name and an order ID without a single explicit flatMap:
import zio.blocks.async._
val summary: String = Async.async {
val user = Async.succeed("Ada").await
val order = Async.succeed(9001).await
s"${user}'s order ${order}"
}.block
println(s"Summary: $summary")
Expected output:
Summary: Ada's order 9001
Async.async { … }opens a macro-powered block; the entire expression produces anAsync[String].- Calling
awaitonAsync.succeed("Ada")extracts"Ada"and binds it touser; this is not a blocking call — the macro rewrites it into aflatMapcontinuation. - Calling
awaitonAsync.succeed(9001)similarly binds the order number toorder. - The final string expression becomes the block's result value.
- The
blockcall drives the whole composed async to completion.
await Is Only Valid Inside Async.async { … }The await method is enforced by the compiler to be used only inside an Async.async { … } block. On Scala 3 the inline expansion fails with:
".await may only be used directly inside an Async.async { ... } block."
On Scala 2, the @compileTimeOnly annotation fires at the same point. Try deleting the Async.async { … } wrapper — the compiler will tell you immediately.
Runnable version: Concept3DirectStyleExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept3DirectStyleExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept3DirectStyleExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Direct Style: `Async.async` and `await`
*
* Sequences two `Async` values in direct style: each `.await` inside
* `Async.async { ... }` is rewritten into a `flatMap` at compile time.
*/
object Concept3DirectStyleExample {
def main(args: Array[String]): Unit = {
val summary: String = Async.async {
val user = Async.succeed("Ada").await
val order = Async.succeed(9001).await
s"${user}'s order ${order}"
}.block
println(s"Summary: $summary")
}
}
6. Bridging Exceptions: Async.attempt
Scala code often signals failure by throwing exceptions rather than returning error values. Async.attempt(body) evaluates a block that may throw and captures any exception as an async failure, turning it into a value that catchAll can recover. A block that succeeds produces a ready-value Async; one that throws produces a failed Async.
The example parses a well-formed string and then a malformed one:
import zio.blocks.async._
val good: Int = Async.attempt("42".toInt).block
println(s"Parsed: $good")
val bad: Either[Throwable, Int] = Async.attempt("oops".toInt).either.block
println(s"Failed parse: $bad")
Expected output:
Parsed: 42
Failed parse: Left(java.lang.NumberFormatException: For input string: "oops")
Async.attempt("42".toInt)evaluates"42".toInt; because it succeeds, the result42becomes a ready-valueAsync[Int].Async.attempt("oops".toInt)evaluates"oops".toInt; theNumberFormatExceptionis caught and becomes a failedAsync.- Chaining
eitherandblockdrives the failed async and reifies its outcome asLeft(…).
Runnable version: Concept4BridgingExceptionsExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept4BridgingExceptionsExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept4BridgingExceptionsExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Bridging Exceptions: `Async.attempt`
*
* Turns a throwing block into an `Async`: a well-formed string parses to a
* ready value, a malformed one becomes a failure that `Async#either` reifies.
*/
object Concept4BridgingExceptionsExample {
def main(args: Array[String]): Unit = {
val good: Int = Async.attempt("42".toInt).block
println(s"Parsed: $good")
val bad: Either[Throwable, Int] = Async.attempt("oops".toInt).either.block
println(s"Failed parse: $bad")
}
}
7. Callback Bridging: Async.promise and Completer
Many real-world APIs — database drivers, network libraries, timers — signal completion by calling a callback rather than returning a value. Async.promise lets you lift these APIs into Async without rewriting them. It suspends the computation and provides a Completer[A] — a thread-safe, one-shot handle — that you can pass to the callback. Calling completer.succeed(value) from any thread resolves the async and wakes up any awaiter.
The example starts a background thread that completes the promise after a short delay:
- Scala 2
- Scala 3
import zio.blocks.async._
val result: String = Async
.promise[String] { c =>
new Thread {
override def run(): Unit = {
Thread.sleep(10)
c.succeed("hello from callback")
}
}.start()
}
.block
println(s"Promise resolved: $result")
import zio.blocks.async._
val result: String = Async
.promise[String] {
val c = summon[Completer[String]]
new Thread {
override def run(): Unit = {
Thread.sleep(10)
c.succeed("hello from callback")
}
}.start()
}
.block
println(s"Promise resolved: $result")
Expected output:
Promise resolved: hello from callback
Async.promise[String] { … }opens the promise body; on Scala 2 theCompleter[String]arrives as an explicit parameterc; on Scala 3 it arrives as a context function argument retrieved withsummon[Completer[String]].- We capture the completer in
cso it can be referenced from the Thread'srun()method — implicits and givens do not propagate across thread boundaries, so we capture explicitly. - Calling
c.succeed("hello from callback")completes the promise; the first call wins and subsequent calls are no-ops. - The
blockcall waits until the completer fires and returns the resolved value.
On Scala 3, Async.promise[String] { (c: Completer[String]) => ... } compiles, but the statements inside the lambda never run, so the completer is never completed and block waits forever. Use the Scala 3 form above, summon[Completer[String]] or { c ?=> ... }. Compiler warning E190 at the call site is the only hint.
Runnable version, the Scala 3 tab: Concept5CallbackBridgingExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept5CallbackBridgingExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept5CallbackBridgingExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Callback Bridging: `Async.promise` and
* `Completer`
*
* The Scala 3 version of the section's code: `summon[Completer[String]]`
* retrieves the completer, which a background thread completes after 10 ms.
*/
object Concept5CallbackBridgingExample {
def main(args: Array[String]): Unit = {
val result: String = Async
.promise[String] {
val c = summon[Completer[String]]
new Thread {
override def run(): Unit = {
Thread.sleep(10)
c.succeed("hello from callback")
}
}.start()
}
.block
println(s"Promise resolved: $result")
}
}
8. Forking and Cancellation: start and Async.Running
By default, async chains run eagerly on the calling thread until the first suspension or completion. To evaluate a block on a background worker (a separate JVM thread or a Scala.js microtask) instead, pass it to Async.start. This returns an Async.Running[A] handle — itself an Async[A] — representing the in-flight computation. You can join it by calling block on the handle, or stop waiting for it with cancel.
The first block forks a computation and joins it, printing the name of the thread each step runs on:
import zio.blocks.async._
val running: Async.Running[Int] = Async.start {
println(s"Computing on ${Thread.currentThread.getName}")
42 * 2
}
val joined: Int = running.block
println(s"Joined on ${Thread.currentThread.getName}: $joined")
Expected output:
Computing on zio-blocks-async-eval
Joined on main: 84
Async.start { … }hands the block to a background worker and returns theRunninghandle immediately; the first line shows the block running on the worker thread,zio-blocks-async-eval.running.blockblocks the calling thread until the background computation finishes and returns the result84, back onmain.
The extension method Async#start drives an Async you have already built. Notice what happens when that Async is a chain of ready values:
import zio.blocks.async._
val ready: Async.Running[Int] = Async
.succeed(42)
.map { x => println(s"map runs on ${Thread.currentThread.getName}"); x * 2 }
.start
println(s"Joined: ${ready.block}")
Expected output:
map runs on main
Joined: 84
- The
mapfunction runs onmain, while the chain is being built and beforestartis called: its input is already available, so there is nothing to wait for. - Calling
starton the result finds a ready value and settles at once, without a background worker. This is the zero-allocation promise from section 2 at work — a chain leaves the calling thread only when it genuinely suspends, such as waiting on aCompleter.
The last block demonstrates cancellation:
import zio.blocks.async._
val running2: Async.Running[Int] = Async.start {
Thread.sleep(100)
println("Body finished after cancel")
99
}
running2.cancel()
println("Cancelled running2")
Thread.sleep(200) // give the body time to finish
Expected output:
Cancelled running2
Body finished after cancel
Async.start { … }forks the block onto a background worker; the call returns theRunninghandle immediately.- Calling
cancelstops the driver, so no result is ever published to an awaiter. The call is idempotent. - The second line shows that
canceldoes not interrupt the block itself: it finishes on its own thread. Cancelling anAsync.startmeans you have stopped waiting for the result, not that the work has stopped.
A cancelled run never settles: it neither succeeds nor fails. Calling block on a cancelled handle waits forever on the JVM and throws IllegalStateException on Scala.js, so cancel only a handle whose every consumer you own. The Async.Running reference covers the details.
Runnable version, with all three blocks of this section: Concept6ForkingAndCancellationExample.scala
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run Concept6ForkingAndCancellationExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.Concept6ForkingAndCancellationExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Forking and Cancellation: `start` and
* `Async.Running`
*
* Forks a block onto a background worker with `Async.start` and joins it, shows
* that `Async#start` on a ready chain settles on the calling thread, and
* cancels a running handle whose body still runs to completion.
*/
object Concept6ForkingAndCancellationExample {
def main(args: Array[String]): Unit = {
val running: Async.Running[Int] = Async.start {
println(s"Computing on ${Thread.currentThread.getName}")
42 * 2
}
val joined: Int = running.block
println(s"Joined on ${Thread.currentThread.getName}: $joined")
val ready: Async.Running[Int] = Async
.succeed(42)
.map { x => println(s"map runs on ${Thread.currentThread.getName}"); x * 2 }
.start
println(s"Joined: ${ready.block}")
val running2: Async.Running[Int] = Async.start {
Thread.sleep(100)
println("Body finished after cancel")
99
}
running2.cancel()
println("Cancelled running2")
Thread.sleep(200) // give the body time to finish
}
}
9. Putting It Together
Let's combine all six concepts into a single order-processing pipeline. The program bridges a callback-based user-lookup API with Async.promise, safely parses an order ID with Async.attempt, runs a stock check in the background with Async.start, sequences everything in direct style inside Async.async, and recovers from any failure with catchAll. Each step reuses a section's code with order-processing values: the callback delivers the user name "Ada" instead of a greeting, Async.attempt parses the order ID "9001", the background block is a stock check that sleeps 5 ms and returns true, and the recovery handler reports the failure's message instead of a fixed default:
//> using scala 3.9.0
//> using dep dev.zio::zio-blocks-async:0.0.56
// Run from this folder: scala-cli run CompleteExample.scala
// Or from async-examples: sbt "runMain zio.blocks.async.gettingstarted.CompleteExample"
package zio.blocks.async.gettingstarted
import zio.blocks.async._
/**
* Getting Started with Async — Putting It Together
*
* An order-processing pipeline assembled from the sections: a callback lookup
* bridged with `Async.promise`, an order ID parsed with `Async.attempt`, a
* stock check forked with `Async.start`, all sequenced in `Async.async` and
* recovered with `Async#catchAll`.
*/
object CompleteExample {
def main(args: Array[String]): Unit = {
// Step 1: Bridge a callback-based user-lookup API with promise + Completer
val userLookup: Async[String] = Async.promise[String] {
val c = summon[Completer[String]]
new Thread {
override def run(): Unit = {
Thread.sleep(10)
c.succeed("Ada")
}
}.start()
}
// Step 2: Parse an order ID that might be malformed
val orderId: Async[Int] = Async.attempt("9001".toInt)
// Step 3: Start a stock-availability check on a background worker
val stockCheck: Async.Running[Boolean] = Async.start { Thread.sleep(5); true }
// Step 4: Compose all steps in direct style and recover from any failure
val report: String = Async.async {
val user = userLookup.await
val id = orderId.await
val inStock = stockCheck.await
s"Order $id for $user: ${if (inStock) "in stock" else "out of stock"}"
}.catchAll { err =>
Async.succeed(s"Order failed: ${err.getMessage}")
}.block
println(s"Report: $report")
}
}
Expected output:
Report: Order 9001 for Ada: in stock
10. Running the Examples
All runnable code from this tutorial is in async-examples/src/main/scala/zio/blocks/async/gettingstarted/.
Run any file from that folder with scala-cli run <File>.scala, or from async-examples with
sbt "runMain zio.blocks.async.gettingstarted.<ObjectName>".
11. What You've Learned
By completing this tutorial, you can now:
- Create ready-value effects with
Async.succeed, transform them withmap, and drive them to a result withblock. - Handle failures with
Async.fail, recover them withcatchAll, and observe both outcomes as data witheither. - Write sequential async pipelines in direct style using
Async.async { … .await … }— the compiler threads theflatMapcalls for you. - Lift throw-based Scala code safely into the async error channel with
Async.attempt. - Bridge a callback-based API with
Async.promiseandCompleter, fork background work withAsync.start, and hold a cancellableAsync.Runninghandle.
12. Where to Go Next
If your code already calls Future-based, Java CompletionStage-based, or callback-based APIs, the next step is How to Wrap Callback and Future-Based APIs in Async, which brings each of them into Async and shows how to cancel the underlying call.
The Async reference page documents every method and combinator with full signatures — it is the natural next stop once you are comfortable with the basics in this tutorial.
If your application manages resources that need deterministic cleanup — database connections, file handles, or connection pools — read the Compile-Time Resource Safety with Scope tutorial, which shows how to tie resource lifetimes to lexical scopes and compose them without try/finally boilerplate.