zio.blocks.async

ZIO Blocks Async — a lightweight asynchronous effect type.

An Async[A] is a (possibly suspended) computation that eventually yields an A or fails with a Throwable. Create one with async.Async.succeed, async.Async.fail, async.Async.attempt, or async.Async.promise; a bare A is not itself an Async[A]. Transform and combine values with the extension methods (map, flatMap, catchAll, await, ...) brought into scope by importing this package.

==Quick start==

 import zio.blocks.async._

 // completes immediately with 42
 val a: Async[Int] = Async.promise[Int](c => c.succeed(42))

 // ops + block for the result
 val n: Int = a.map(_ + 1).block

Attributes

Members list

Packages

Type members

Classlikes

object Async

Constructors for Async values.

Constructors for Async values.

An Async[A] is created with one of succeed, fail, attempt, or promise (a bare A is not itself an Async[A]). The transformation and combination operators (map, flatMap, catchAll, await, ...) become available as extension methods on Async[A] after importing zio.blocks.async._.

Attributes

Source
Async.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Async.type
object AsyncSelector

Attributes

Companion
class
Source
AsyncSelector.scala
Supertypes
class Object
trait Matchable
class Any
Self type
final class AsyncSelector[A] extends Cancelable

A repeatable, round-robin selector for independently pending computations.

A repeatable, round-robin selector for independently pending computations.

Eligibility means that a slot is armed (initially, or by replace) and its computation has completed. Among eligible slots selection scans cyclically from the slot after the previous winner. Consequently a continuously eligible slot wins within at most armedCount successful selections.

The selector registers one stable waker per armed slot, rather than a new loser waker per call to select. A winning slot is disarmed atomically and cannot win twice. shutdown is idempotent and does not finish until cleanup for every still-armed loser has finished.

Attributes

Companion
object
Source
AsyncSelector.scala
Supertypes
trait Cancelable
class Object
trait Matchable
class Any
case object BuildInfo

This object was generated by sbt-buildinfo.

This object was generated by sbt-buildinfo.

Attributes

Source
BuildInfo.scala
Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
BuildInfo.type
object Cancelable

Companion for Cancelable, providing the no-op handle.

Companion for Cancelable, providing the no-op handle.

Attributes

Companion
trait
Source
Cancelable.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Cancelable.type
trait Cancelable extends AutoCloseable

Handle returned by Async.start (via Async.Running) that stops a running Async.

Handle returned by Async.start (via Async.Running) that stops a running Async.

Calling cancel() is idempotent and synchronous. It prevents a terminal value from being published if cancellation linearizes before the run reaches completion, and it is a no-op once the run has completed.

When cancellation wins, the driver also signals Pollable.cancel on the active pending operation. A source that can abort an in-flight socket read, timer, or callback registration implements that hook; the default hook is a no-op. Cancellation does not wait for an already-running Pollable.poll invocation to return.

Extends java.lang.AutoCloseable so a Async.Running handle can be used as a managed resource (scala.util.Using, Java try-with-resources): close simply delegates to cancel(). cancel remains the canonical verb — it names the domain operation (stop an in-flight computation) and, unlike AutoCloseable.close, declares no checked exception.

Attributes

Companion
object
Source
Cancelable.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Running[A]
class AsyncSelector[A]
final class Completer[A] extends Pollable[A]

The completion capability handed to an Async.promise block: complete the resulting Async by calling succeed (with a value) or fail (with a cause), typically from an I/O callback.

The completion capability handed to an Async.promise block: complete the resulting Async by calling succeed (with a value) or fail (with a cause), typically from an I/O callback.

Thread-safe and '''one-shot''': the first call to succeed or fail wins; all subsequent calls are silent no-ops, regardless of which thread they come from.

Attributes

Source
Completer.scala
Supertypes
class Pollable[A]
class Object
trait Matchable
class Any
final class Failure extends Pollable[Nothing]

Failure outcome of an Async. Constructed via Async.fail (or by Completer.fail, or captured from thrown code by Async.attempt); recovered via the .catchAll / .mapError / .orElse extensions or surfaced as a thrown Throwable by .block.

Failure outcome of an Async. Constructed via Async.fail (or by Completer.fail, or captured from thrown code by Async.attempt); recovered via the .catchAll / .mapError / .orElse extensions or surfaced as a thrown Throwable by .block.

A failure is terminal: once an Async has failed, it stays failed with the same cause unless a handler such as .catchAll recovers it.

NOTE: errors thrown by user code inside .map(f) / .flatMap(f) are NOT captured — Async is eager, so a throw in f escapes through the call site before any later .catchAll runs. To convert thrown exceptions into a Failure, wrap the work in Async.attempt.

Attributes

Source
Failure.scala
Supertypes
class Pollable[Nothing]
class Object
trait Matchable
class Any
abstract class Pollable[+A]

The implementation point for a custom suspended Async leaf (a timer, a socket read, a callback bridge, ...). A Pollable[A] is itself an Async[A], so it can be used wherever an Async[A] is expected.

The implementation point for a custom suspended Async leaf (a timer, a socket read, a callback bridge, ...). A Pollable[A] is itself an Async[A], so it can be used wherever an Async[A] is expected.

Implementors define poll: when asked for its value, a pollable either returns the completed result (a value, or a failed Async) or, if it is still pending, stashes the supplied java.lang.Runnable and returns a pending Async (typically itself). When the value later becomes available it must invoke onComplete.run() to ask the scheduler to poll again.

Attributes

Source
Pollable.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Running[A]
class Completer[A]
class Failure

Types

type Async[+A] = encoding.Async[A]

A possibly-suspended computation that will eventually yield an A (or fail with a Throwable). A async.Pollable may be used wherever an Async[A] is expected.

A possibly-suspended computation that will eventually yield an A (or fail with a Throwable). A async.Pollable may be used wherever an Async[A] is expected.

Async is a restricted monad: A must not itself be an Async (nor a async.Pollable). Compose effects with flatMap rather than nesting them as success values; Async[Async[A]] is unsupported.

Attributes

Source
package.scala

Value members

Inherited methods

def fail[A](cause: Throwable)(using c: Completer[A]): Unit

Complete the surrounding Async.promise block with a failure.

Complete the surrounding Async.promise block with a failure.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
def succeed[A](value: A)(using c: Completer[A]): Unit

Complete the surrounding Async.promise block with value.

Complete the surrounding Async.promise block with value.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def unless(inline cond: Boolean)(inline fa: encoding.Async[Any]): encoding.Async[Unit]

Inverse of when: evaluate fa only when cond is false.

Inverse of when: evaluate fa only when cond is false.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def when(inline cond: Boolean)(inline fa: encoding.Async[Any]): encoding.Async[Unit]

Conditionally evaluate fa. Returns fa.unit when cond is true, Async.succeed(()) otherwise. The unevaluated fa is by-name so it is not constructed when the condition is false.

Conditionally evaluate fa. Returns fa.unit when cond is true, Async.succeed(()) otherwise. The unevaluated fa is by-name so it is not constructed when the condition is false.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala

Extensions

Inherited extensions

extension [A](fa: encoding.Async[A])
def toCompletableFuture(using ec: ExecutionContext): CompletableFuture[A]

Convert fa into a java.util.concurrent.CompletableFuture that completes with the same value or error, for Java-shaped consumers.

Convert fa into a java.util.concurrent.CompletableFuture that completes with the same value or error, for Java-shaped consumers.

Attributes

Inherited from:
AsyncSyntaxPlatformSpecific (hidden)
Source
AsyncSyntaxPlatformSpecific.scala
def toFuture(using ec: ExecutionContext): Future[A]

Convert fa into a scala.concurrent.Future that completes with the same value or error. A not-yet-complete fa is driven on ec, so this returns immediately and the future completes when fa does.

Convert fa into a scala.concurrent.Future that completes with the same value or error. A not-yet-complete fa is driven on ec, so this returns immediately and the future completes when fa does.

Attributes

Inherited from:
AsyncSyntaxPlatformSpecific (hidden)
Source
AsyncSyntaxPlatformSpecific.scala
extension [A](inline fa: encoding.Async[A])
inline def *>[B](inline that: encoding.Async[B]): encoding.Async[B]

Sequence then return that's value (zipRight, ZIO's *>).

Sequence then return that's value (zipRight, ZIO's *>).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def <*[B](inline that: encoding.Async[B]): encoding.Async[A]

Sequence then return fa's value (zipLeft, ZIO's &lt;*).

Sequence then return fa's value (zipLeft, ZIO's &lt;*).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def as[B](inline b: B): encoding.Async[B]

Replace the value with b. Equivalent to fa.map(_ => b).

Replace the value with b. Equivalent to fa.map(_ => b).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def await: A

Direct-style await: extract the value of fa without blocking, usable '''only''' directly inside an Async.async { ... } block. Using .await anywhere else is a compile error.

Direct-style await: extract the value of fa without blocking, usable '''only''' directly inside an Async.async { ... } block. Using .await anywhere else is a compile error.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def block: A

Drive fa to its value, blocking the calling thread until it is ready. A failure is re-thrown as its cause.

Drive fa to its value, blocking the calling thread until it is ready. A failure is re-thrown as its cause.

This is the unsafe escape hatch: ready values return immediately; a pending value blocks the (Loom-friendly) calling thread on the JVM, and on JS — which cannot block — throws. Inside an Async.async { ... } block use the direct-style await instead, which runs without blocking.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def catchAll[A1 >: A](inline f: Throwable => encoding.Async[A1]): encoding.Async[A1]

Recover from a failure by applying f to its cause. A successful fa is returned unchanged (the handler is not invoked).

Recover from a failure by applying f to its cause. A successful fa is returned unchanged (the handler is not invoked).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def either: encoding.Async[Either[Throwable, A]]

Convert any Failure into a Left; any value into a Right.

Convert any Failure into a Left; any value into a Right.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def ensuring(inline finalizer: encoding.Async[Any]): encoding.Async[A]

Run finalizer after fa completes — success, failure, or suspension — and propagate the original outcome. A failure in finalizer is suppressed (the original outcome wins).

Run finalizer after fa completes — success, failure, or suspension — and propagate the original outcome. A failure in finalizer is suppressed (the original outcome wins).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def flatMap[B](inline f: A => encoding.Async[B]): encoding.Async[B]

Sequence fa with f: when fa succeeds, continue with f applied to its value. A failure short-circuits and is propagated unchanged.

Sequence fa with f: when fa succeeds, continue with f applied to its value. A failure short-circuits and is propagated unchanged.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def foldCause[B](inline onFailure: Throwable => B)(inline onSuccess: A => B): encoding.Async[B]

Fold a possibly-failed Async into a guaranteed-success value by handling both branches. Equivalent to fa.map(onSuccess).catchAll(t => Async.succeed(onFailure(t))).

Fold a possibly-failed Async into a guaranteed-success value by handling both branches. Equivalent to fa.map(onSuccess).catchAll(t => Async.succeed(onFailure(t))).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def map[B](inline f: A => B): encoding.Async[B]

Transform the success value of fa with f. A failure short-circuits and is propagated unchanged.

Transform the success value of fa with f. A failure short-circuits and is propagated unchanged.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def mapError(inline f: Throwable => Throwable): encoding.Async[A]

Transform the cause of any Failure; values pass through.

Transform the cause of any Failure; values pass through.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def tap(inline f: A => encoding.Async[Any]): encoding.Async[A]

Run f for its effect when fa succeeds, then yield fa's original value. A failure in fa (or in f) is propagated.

Run f for its effect when fa succeeds, then yield fa's original value. A failure in fa (or in f) is propagated.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def unit: encoding.Async[Unit]

Discard the value. Equivalent to fa.as(()).

Discard the value. Equivalent to fa.as(()).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def zip[B](inline that: encoding.Async[B])(using t: Tuples[A, B]): encoding.Async[t.Out]

Sequentially combine fa with that, fusing the values via the combinators Tuples combiner: a zip b zip c flattens to Async[(A, B, C)] rather than Async[((A, B), C)]. Unit on either side is erased by the Tuples instances; tuple-on-tuple inputs are heterogeneously-concatenated.

Sequentially combine fa with that, fusing the values via the combinators Tuples combiner: a zip b zip c flattens to Async[(A, B, C)] rather than Async[((A, B), C)]. Unit on either side is erased by the Tuples instances; tuple-on-tuple inputs are heterogeneously-concatenated.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
inline def zipWith[B, C](inline that: encoding.Async[B])(inline f: (A, B) => C): encoding.Async[C]

Combine fa with that using f. The two are sequenced strictly left-to-right: fa is driven to a value first, and only then is that driven. A failure in fa short-circuits without driving that; a failure in that is surfaced only after fa has succeeded.

Combine fa with that using f. The two are sequenced strictly left-to-right: fa is driven to a value first, and only then is that driven. A failure in fa short-circuits without driving that; a failure in that is surfaced only after fa has succeeded.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
extension [A](fa: encoding.Async[A])
def orElse[B](that: => encoding.Async[B])(using c: Concat[A, B]): encoding.Async[c.Out]

Fall back to that if fa fails, widening the two success types through the combinators Concat instance: on Scala 3 the result is the native union A | B (so Async[Int].orElse(Async[String]) is Async[Int | String]), rather than collapsing both sides to their least upper bound. that is by-name, so the fallback is built only on failure.

Fall back to that if fa fails, widening the two success types through the combinators Concat instance: on Scala 3 the result is the native union A | B (so Async[Int].orElse(Async[String]) is Async[Int | String]), rather than collapsing both sides to their least upper bound. that is by-name, so the fallback is built only on failure.

The common case is allocation-free: when Concat.isIdentityLike (always so for a union, and for same-type / sub-/supertype / shared-supertype derivations), both sides are already valid Out values, so we skip the per-value left/right projections (no map wrappers) and just recover with a cast — for a ready fa this collapses to a no-op. Only a disjoint Scala 2 Either widening (isIdentityLike == false) actually projects each side through c.left / c.right.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
def start: Running[A]

Eagerly drive an already-built fa and return a Async.Running handle. Separate (non-inline) extension so it can reach the in-package internal.AsyncRunner driver directly — driving an existing Async is distinct from the companion Async.start(body), which evaluates a by-name body (Future.apply-style).

Eagerly drive an already-built fa and return a Async.Running handle. Separate (non-inline) extension so it can reach the in-package internal.AsyncRunner driver directly — driving an existing Async is distinct from the companion Async.start(body), which evaluates a by-name body (Future.apply-style).

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala
extension [A](inline ffa: encoding.Async[encoding.Async[A]])
inline def flatten: encoding.Async[A]

Flatten an Async[Async[A]] one level.

Flatten an Async[Async[A]] one level.

Attributes

Inherited from:
AsyncSyntaxVersionSpecific (hidden)
Source
AsyncSyntaxVersionSpecific.scala