Asynchronous Programming Primitives #
This module provides a layered approach to asynchronous programming, combining monadic types, type classes, and concrete task types that work together in a cohesive system.
- Monadic Types: These types provide a good way to to chain and manipulate context. These
can contain a
Task
, enabling manipulation of both asynchronous and synchronous code. - Concrete Task Types: Concrete units of work that can be executed within these contexts.
Monadic Types #
These types provide a good way to to chain and manipulate context. These can contain a Task
,
enabling manipulation of both asynchronous and synchronous code.
BaseAsync
: A monadic type for infallible asynchronous computationsEAsync
: A monadic type for asynchronous computations that may fail with an error of typeε
Async
: A monadic type for IO-based asynchronous computations that may fail withIO.Error
(alias forEAsync IO.Error
)
Concurrent Units of Work #
These are the concrete computational units that exist within the monadic contexts. These types should not be created directly.
Task
: A computation that will resolve to a value of typeα
,ETask
: A task that may fail with an error of typeε
.AsyncTask
: A task that may fail with anIO.Error
(alias forETask IO.Error
).
Relation #
These types are related by two functions in the type classes MonadAsync
and MonadAwait
: async
and await
. The async
function extracts a concrete asynchronous task from a computation within the
monadic context. In effect, it runs the computation in the background and returns a task handle that
can be awaited later. On the other hand, the await
function takes a task and re-inserts it into the
monadic context, allowing its result to be composed using monadic bind and also pausing to wait for that result.
This relationship between async
and await
enables precise control over when a computation begins
and when its result is used. You can spawn multiple asynchronous tasks using async
, perform other
operations in the meantime, and later rejoin the computation flow by awaiting their results.
These functions should not be used directly. Instead, prefer higher-level combinators such as
race
, raceAll
, concurrently
, background
and concurrentlyAll
. The best way to think about
how to write your async code, it to avoid using the concurrent units of work, and only use it when integrating
with non async code that uses them.
Typeclass for monads that can "await" a computation of type t α
in a monad m
until the result is
available.
- await {α : Type} : t α → m α
Awaits the result of
t α
and returns it inside them
monad.
Instances
Represents monads that can launch computations asynchronously of type t
in a monad m
.
Starts an asynchronous computation in another monad.
Instances
Equations
Equations
Equations
Equations
Equations
Equations
Equations
Equations
A Task
that may resolve to either a value of type α
or an error value of type ε
.
Equations
Instances For
Creates a new ETask
that will run after x
has finished. If x
:
Equations
Instances For
Creates a new ETask
that will run after x
has completed. If x
:
- errors, return an
ETask
that resolves to the error. - succeeds, run
f
on the result ofx
and return theETask
produced byf
.
Equations
Instances For
Similar to bind
, however f
has access to the EIO
monad. If f
throws an error, the returned
ETask
resolves to that error.
Equations
Instances For
Similar to bind
, however f
has access to the EIO
monad. If f
throws an error, the returned
ETask
resolves to that error.
Equations
Instances For
Similar to map
, however f
has access to the IO
monad. If f
throws an error, the returned
AsyncTask
resolves to that error.
Equations
Instances For
Create a new AsyncTask
that will run after x
has finished.
If x
:
- errors, return an
AsyncTask
that resolves to the error. - succeeds, run
f
on the result ofx
and return theAsyncTask
produced byf
.
Equations
Instances For
Create a new AsyncTask
that will run after x
has finished.
If x
:
- errors, return an
AsyncTask
that resolves to the error. - succeeds, return an
AsyncTask
that resolves tof x
.
Equations
Instances For
Similar to bind
, however f
has access to the IO
monad. If f
throws an error, the returned
AsyncTask
resolves to that error.
Equations
Instances For
Similar to map
, however f
has access to the IO
monad. If f
throws an error, the returned
AsyncTask
resolves to that error.
Equations
Instances For
Create an AsyncTask
that resolves to the value of x
.
Equations
Instances For
A MaybeTask α
represents a computation that either:
- Is immediately available as an
α
value, or - Is an asynchronous computation that will eventually produce an
α
value.
Instances For
Maps a function over a MaybeTask
.
Equations
Instances For
Sequences two computations, allowing the second to depend on the value computed by the first.
Equations
Instances For
An asynchronous computation that never fails.
Equations
Instances For
Creates a BaseAsync
computation that immediately returns the given value.
Equations
Instances For
Maps the result of a BaseAsync
computation with a function.
Equations
Instances For
Sequences two computations, allowing the second to depend on the value computed by the first.
Equations
Instances For
Equations
Instances For
Lifts a BaseAsync
computation into a Task
that can be awaited and joined.
Equations
Instances For
Returns the BaseAsync
computation inside a Task α
, so it can be awaited.
Equations
Instances For
Equations
Equations
Equations
An asynchronous computation that may produce an error of type ε
.
Equations
Instances For
Creates an EAsync
computation that immediately returns the given value.
Equations
Instances For
Lifts an EAsync
computation into an ETask
that can be awaited and joined.
Equations
Instances For
Handles errors in an EAsync
computation by running a handler if one occurs.
Equations
Instances For
Runs an action, ensuring that some other action always happens afterward.
Equations
Instances For
Returns the EAsync
computation inside an ETask ε α
, so it can be awaited.
Equations
Instances For
Equations
Equations
Equations
Equations
Equations
Equations
Equations
Equations
Equations
Equations
Instances For
An asynchronous computation that may produce an error of type IO.Error
..
Equations
Instances For
Equations
Equations
Equations
This function transforms the operation inside the monad m
into a task and let it run in the background.
Equations
Instances For
Runs two computations concurrently and returns both results as a pair.
Equations
Instances For
Runs two computations concurrently and returns the result of the one that finishes first. The other result is lost and the other task is not cancelled, so the task will continue the execution until the end.
Equations
Instances For
Runs all computations in an Array
concurrently and returns all results as an array.
Equations
Instances For
Runs all computations concurrently and returns the result of the first one to finish. All other results are lost, and the tasks are not cancelled, so they'll continue their executing until the end.