import { Task } from "https://deno.land/x/eitherway@0.10.0/mod.ts";
Task<T, E>
Task<T, E>
is a composeable extension of Promise<Result<T, E>>
It is a Promise
sub-class, which never rejects, but always resolves.
Either with an Ok<T>
or an Err<E>
It supports almost the same API as Result
and allows for
the same composition patterns as Result
Furthermore, Tasks
exposes a few functions to ease working
with collections (indexed and plain Iterable
s)
Constructors
Properties
This well-known symbol is called by Object.prototype.toString
to
obtain a string representation of a value's type
This maybe useful for debugging or certain logs
The [.toString()
]this#toString method is a useful short-hand in these scenarios
See the reference
Methods
Use this to conditionally pass-through the encapsulated value <T>
based upon the outcome of the supplied ensureFn
.
In case of Err<E>
, this method short-circuits.
In case of Ok<T>
, the supplied ensureFn
is called with the encapsulated
value <T>
and if the return value is:
Ok<T2>
: it is discarded and the originalOk<T>
is returnedErr<E2>
:Err<E2>
is returned
See Task#orEnsure
for the opposite case.
This is equivalent to chaining:
original.andThen(ensureFn).and(original)
LHS andEnsure RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Err |
LHS: Err | Err | Err |
Use this to return the Task
itself. Canonical identity function.
Mostly useful for flattening or en lieu of a noop.
This is mostly provided for compatibility with with Result<T, E>
.
Use this to obtain an async iterator of the encapsulated value <T>
In case of failure, this method returns the empty AsyncIteratorResult
Use this to conditionally pass-through the encapsulated value <E>
based upon the outcome of the supplied ensureFn
.
In case of Ok<T>
, this method short-circuits.
In case of Err<E>
, the supplied ensureFn
is called with the encapsulated
value <E>
and if the return value is:
Ok<T2>
: it is returnedErr<T2>
: it is discarded and the originalErr<E>
is returned
See Task#andEnsure
for the opposite case.
This is equivalent to chaining:
original.orElse(ensureFn).or(original)
LHS orEnsure RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Ok |
LHS: Err | Ok | Err |
Use this to get the full string tag
Short-hand for Object.prototype.toString.call(task)
See the reference
In case of success AND that the encapsulated value <T>
implements the
async iterator protocol, this delegates to the underlying implementation
In all other cases, it yields the empty AsyncIteratorResult
Static Methods
Use this to create a deferred Task<T, E>
which will either succeed with
a value of type <T>
or fail with a value of type <E>
You have to provide the generic types explicitly, otherwise <T, E>
will
be inferred as <unknown, unknown>
This is mostly useful when working with push-based APIs
Use this to create a task from a function which returns a Result<T, E>
or PromiseLike<Result<T, E>
value.
This function should be infallible by contract.
Use Task.fromFallible
if this is not the case.
Use this to create a Task<T, E>
from a Promise<T>
.
You have to provide an errorMapFn
in case the promise rejects, so that
the type can be inferred.
If you are certain(!) that the provided promise will never reject, you can
provide the asInfallible
helper from the core module.
Use this lift a function into a Task
context, by composing the wrapped
function with a Result
constructor and an error mapping function.
If no constructor is provided, Ok
is used as a default.
This higher order function is especially useful to intergrate 3rd party
code into your Task
pipelines.