Task — Lazy Asynchronous Work
In modern JavaScript, asynchronous operations are modeled using Promise objects. Promises are highly convenient runtime constructs, but they have two specific design characteristics that make them challenging to compose cleanly:
- Promises are eager: The moment a Promise is created, the underlying work starts executing.
- Promises are fallible: A Promise can reject, throwing an untyped exception that bypasses the static type system.
Because a Promise is eager, you cannot build a pipeline of asynchronous transformations and pass it around as a pure, inactive description before any work actually begins. By the time you have the Promise in hand, the network request is already in flight.
Because Promises can reject, failure leaks out as an untyped runtime exception. This forces you to write defensive try/catch blocks at call sites, and makes it impossible to tell from a function’s type signature whether it can fail.
Task<A> solves both problems. A Task is simply a zero-argument function that returns a Deferred<A>:
Deferred<A> is a minimal, infallible asynchronous value. It supports await but has no .catch() or .finally() methods. At the type level, a Deferred value is guaranteed never to reject.
By wrapping our async work in a function, we make it lazy—nothing runs until you explicitly invoke the Task. And by treating the core Task as infallible, we force all possible failures into the data channel: TaskResult<E, A> is a Task yielding a Result<E, A>, making failures impossible to overlook.
Creating Tasks
Section titled “Creating Tasks”We lift asynchronous values into a Task context depending on whether they are already resolved or require evaluation:
Task.from is a clear, explicit alias for wrapping a promise factory () => Promise<A>. It serves as the primary gateway for lifting standard async library calls into our functional system.
Transforming and Sequencing
Section titled “Transforming and Sequencing”We can transform and chain our asynchronous blueprints while they are still in their lazy, unexecuted state.
Transforming values with map
Section titled “Transforming values with map”map describes how the resolved value should change once the Task eventually runs, returning a new Task without triggering any execution:
Sequential chains with chain
Section titled “Sequential chains with chain”When a transformation itself returns another Task, we use chain to execute them sequentially and flatten the resulting context:
Concurrency Controls
Section titled “Concurrency Controls”Unlike raw Promises, which require manual coordination using Promise.all or Promise.race, Task provides clean, functional combinators for parallel, raced, or sequential execution.
Parallel execution with all
Section titled “Parallel execution with all”Task.all runs an array of Tasks simultaneously and collects all results into a typed tuple:
The return type is structurally matched to your input: passing [Task<Config>, Task<User>] yields a Task<[Config, User]> tuple.
Raced execution with race
Section titled “Raced execution with race”Task.race starts multiple Tasks simultaneously and resolves with the outcome of whichever Task completes first, abandoning the remaining in-flight tasks:
Sequential queueing with sequential
Section titled “Sequential queueing with sequential”When execution order matters, or when running tasks in parallel would trigger race conditions or overload resources, Task.sequential executes each Task in submission order:
Operational Utilities
Section titled “Operational Utilities”Delayed execution with delay
Section titled “Delayed execution with delay”Task.delay introduces a timed pause before the Task executes:
Standard cancellation with abortable
Section titled “Standard cancellation with abortable”Task.abortable wraps a promise factory, yielding a managed Task alongside a shared abort function. Invoking abort() immediately cancels any active, in-flight execution:
Calling task() while a previous invocation is still active will automatically abort the previous run.
Ending pipelines with run
Section titled “Ending pipelines with run”When composing inline pipelines, Task.run allows you to execute the Task at the terminal end of the pipe chain, accepting an optional AbortSignal for external cancellation:
Polling and Repetition
Section titled “Polling and Repetition”Task provides built-in utilities to repeat executing a task, designed naturally around the guarantee that Tasks are infallible.
repeat executes a Task a fixed number of times, collecting all outcomes:
repeatUntil polls a Task repeatedly on an interval until the returned value satisfies a predicate:
The Task Family
Section titled “The Task Family”While Task<A> is excellent for operations that never fail, typical real-world async tasks involve network or database requests that can yield errors. For these, we use specialized variants within the Task family.
TaskResult
Section titled “TaskResult”TaskResult<E, A> represents a fallible async task, equivalent to Task<Result<E, A>> under the hood. It serves as the primary tool for most asynchronous applications:
Cancellation propagation in TaskResult chains
Section titled “Cancellation propagation in TaskResult chains”When sequencing multiple TaskResult steps, TaskResult.chain propagates the abort signal down the line automatically. An abort triggered at the call site immediately cancels whichever step is currently in flight:
TaskMaybe
Section titled “TaskMaybe”TaskMaybe<A> represents an asynchronous operation that may yield nothing, equivalent to Task<Maybe<A>>:
Accumulating values: bind / bindTo
Section titled “Accumulating values: bind / bindTo”When you need to perform multiple sequential asynchronous operations and gather their results into a single object, nesting chain and map inside pipelines can become highly complex and hard to read:
To solve this, you can use bindTo and bind to cleanly accumulate asynchronous values key-by-key in a flat, readable pipeline. These helpers are available across the entire Task family: Task, TaskResult, and TaskMaybe.
bindTo lifts an asynchronous value into the pipeline’s accumulator object:
bind runs a new asynchronous operation using the accumulated object and attaches the result to a new key:
If any step fails (yielding None in TaskMaybe or Err in TaskResult), the entire pipeline short-circuits and propagates the failure immediately.
Combining async records: struct
Section titled “Combining async records: struct”While bind is perfect for sequential steps where a latter step depends on the output of a prior step, sometimes you have a set of independent asynchronous tasks that you want to combine into a single object. For this, you can use TaskResult.struct.
It combines a record of TaskResults into a single TaskResult holding a record of success values. Under the hood, it evaluates all tasks in parallel (by calling them concurrently and using Promise.all internally) while correctly forwarding the AbortSignal down to each sub-task:
If any individual field resolves to an Err, the entire struct short-circuits to that error. If multiple fields fail, TaskResult.struct returns the first error encountered in key order:
Running Tasks at boundaries
Section titled “Running Tasks at boundaries”Because a Task is simply a function, you run it by invoking it. Invoking a Task<A> yields a Deferred<A>, which is highly compatible with the standard Promise ecosystem. You can await it directly within any standard async/await block:
If you are interfacing with external libraries or frameworks that strictly require a standard Promise instance, you can convert the Deferred value using Deferred.toPromise:
When to use Task vs async/await
Section titled “When to use Task vs async/await”Use Task when:
Section titled “Use Task when:”- You need composition: You want to define, combine, or delay asynchronous steps point-free inside
pipechains before triggering execution. - You need concurrency controls: You require parallel, raced, or ordered sequential queues (
Task.all,Task.race,Task.sequential). - You want typed errors: You want compiler-enforced error handling via
TaskResultrather than catching untyped exceptions in standardasync/awaitblocks.
Keep using async/await when:
Section titled “Keep using async/await when:”- Operations are standalone: Simple, linear scripts or internal functions where functional composition and pipelining add zero architectural value.