Task
Task
is an abstraction in F-Box that represents asynchronous computations. It encapsulates functions returning promises, enabling composable and error-safe asynchronous workflows.
The Task
abstraction simplifies the management of asynchronous code, offering a functional interface for transforming, chaining, and handling errors in computations.
Key Features
- Encapsulation: Wraps asynchronous computations into a
Task
object. - Composability: Provides operators and methods (
map
,flatMap
,apply
) for chaining computations. - Error Handling: Includes utilities like
tryCatch
andtryTask
for managing errors. - Functional Interface: Adheres to functional programming principles, ensuring immutability and composability.
Creating a Task
The Task
abstraction provides multiple ways to create tasks:
- From a Promise: Use
Task.from
to encapsulate a promise. - From a Value: Use
Task.pack
to lift a value into a resolvedTask
. - Error Handling: Use
Task.tryCatch
to create aTask
with custom error recovery logic.
Example
Supported Operators
The Task
abstraction provides several operators for working with asynchronous computations:
<$>
: Alias formap
. Transforms the result of aTask
.<*>
: Alias forapply
. Applies aTask
containing a function to aTask
containing a value.>>=
: Alias forflatMap
. Chains computations that return newTask
instances.
For detailed usage, see API Methods.
API Methods
Task.from
Task.from<T>(fn: () => Promise<T>): Task<T>
Creates a Task
from a promise-returning function.
Task.pack
Task.pack<T>(value: T): Task<T>
Creates a Task
that resolves immediately with the given value.
Task.pack
is a shorthand for Task.from(() => Promise.resolve(value))
. It provides a quick way to lift a value into a Task
for immediate resolution.
Task.tryCatch
Task.tryCatch<T>(fn: () => T | Promise<T>, onError: (error: any) => T | Promise<T>): Task<T>
Creates a Task
that handles errors using a recovery function.
Task.tryTask
Task.tryTask<T>(fn: () => T | Promise<T>): Task<T>
Creates a Task
that propagates any errors that occur.
map
map<U>(fn: (value: T) => U): Task<U>
Transforms the result of a Task
using the provided function.
Alias: <$>
flatMap
flatMap<U>(fn: (value: T) => Task<U>): Task<U>
Chains another Task
based on the result of this Task
and flattens the result.
Alias: >>=
apply
apply<U, V>(this: Task<(value: U) => V>, taskValue: Task<U>): Task<V>
Applies a Task
containing a function to a Task
containing a value and returns a new Task
.
Alias: <*>
run
run(): Promise<T>
Executes the asynchronous computation and returns a Promise
resolving with the result.
Helper Methods
Task.isTask
Task.isTask(value: any): boolean
Checks if a given value is a Task
.
Use Cases
Transforming Asynchronous Results
Transform the result of an asynchronous computation:
This demonstrates how to:
- Use
Task.pack
to wrap an initial value. - Apply successive transformations to the value using
["<$>"]
.
Error Recovery
Handle errors with recovery logic:
This demonstrates how to:
- Use
Task.tryCatch
to execute a computation that might fail. - Provide a fallback recovery function to handle errors gracefully.
Chaining Asynchronous Computations
Chain multiple asynchronous operations, starting from a context value using Task.pack
:
This demonstrates how to:
- Start with a context value using
Task.pack
. - Chain asynchronous operations using
["">>="]
to handle sequential dependencies. - Use
Task.tryCatch
to ensure resilience in each step.
Running Parallel Asynchronous Tasks
Execute multiple asynchronous operations in parallel and combine their results:
This demonstrates how to:
- Use
Task.from
to encapsulate multiple asynchronous operations. - Combine the results of parallel computations using
["<*>"]
. - Run the resulting
Task
to execute all operations concurrently and retrieve the combined results.
Why Use Task?
The Task
abstraction provides a powerful framework for managing asynchronous workflows. Its composable interface simplifies error handling, transformation, and chaining, making it an essential tool for modern JavaScript applications.
Next Steps
Explore other abstractions in F-Box: