Construct a new Task
, using callbacks to wrap APIs which do not natively
provide a Promise
.
This is identical to the Promise constructor, with one very
important difference: rather than producing a value upon resolution and
throwing an exception when a rejection occurs like Promise
, a Task
always “succeeds” in producing a usable value, just like Result
for synchronous code.
For constructing a Task
from an existing Promise
, see:
For constructing a Task
immediately resolved or rejected with given
values, see Task.resolve
and Task.reject
respectively.
Static
fromProduce a Task<T, E>
from a promise of a Result<T, E>
.
This constructor assumes you have already correctly handled the promise
rejection state, presumably by mapping it into the wrapped Result
. It is
unsafe for this promise ever to reject! You should only ever use this
with Promise<Result<T, E>>
you have created yourself (including via a
Task
, of course).
For any other Promise<Result<T, E>>
, you should first attach a catch
handler which will also produce a Result<T, E>
.
If you call this with an unmanaged Promise<Result<T, E>>
, that is, one
that has not correctly set up a catch
handler, the rejection will
throw an UnsafePromise
error that will not be catchable
by awaiting the Task
or its original Promise
. This can cause test
instability and unpredictable behavior in your application.
Use the module-scoped fromUnsafePromise
instead.
Static
rejectConstruct a Task
which is already rejected. Useful when you have an error
already, but need it to be available in an API which expects a Task
.
Construct a Task
which is already rejected. Useful when you have an error
already, but need it to be available in an API which expects a Task
.
Static
resolveConstruct a Task
which is already resolved. Useful when you have a value
already, but need it to be available in an API which expects a Task
.
Construct a Task
which is already resolved. Useful when you have a value
already, but need it to be available in an API which expects a Task
.
Static
tryProduce a Task<T, unknown>
from a promise.
To handle the error case and produce a concrete Task<T, E>
instead, use
the overload which accepts an onRejection
handler instead.
The promise from which to create the Task
.
This will be removed at 9.0. Switch to the module-scoped
function fromPromise
.
Static
tryProduce a Task<T, E>
from a Promise<T>
and use a fallback
value if the task rejects, ignoring the rejection reason.
Notes:
unknown
, use the overload which accepts only the
promise.This will be removed at 9.0. Switch to the module-level function
safelyTryOr
, which accepts a callback instead.
Static
tryProduce a Task<T, E>
from a Promise<T>
and a function to transform an
unknown error to E
.
To leave any error as unknown
, use the overload which accepts only the
promise.
This will be removed at 9.0. Switch to the module-level function
safelyTryOrElse
, which accepts a callback instead.
Static
withCreate a pending Task
and supply resolveWith
and rejectWith
helpers,
similar to the Promise.withResolvers
static method, but producing a
Task
with the usual safety guarantees.
let { task, resolveWith, rejectWith } = Task.withResolvers<string, Error>();
resolveWith("Hello!");
let result = await task.map((s) => s.length);
let length = result.unwrapOr(0);
console.log(length); // 5
let { task, resolveWith, rejectWith } = Task.withResolvers<string, Error>();
rejectWith(new Error("oh teh noes!"));
let result = await task.mapRejection((s) => s.length);
let errLength = result.isErr ? result.error : 0;
console.log(errLength); // 5
Attaches callbacks for the resolution and/or rejection of the Promise.
A Promise for the completion of which ever callback is executed.
Static
fromBuild a Task<T, E>
from a Result<T, E>
.
Use the module-scoped fromResult
instead.
A
Task
is a type safe asynchronous computation.You can think of a
Task<T, E>
as being basically aPromise<Result<T, E>>
, because it is aPromise<Result<T, E>>
under the hood, but with two main differences from a “normal”Promise
:A
Task
cannot “reject”. All errors must be handled. This means that, like aResult
, it will never throw an error if used in strict TypeScript.Unlike
Promise
,Task
robustly distinguishes betweenmap
andandThen
operations.Task
also implements JavaScript’sPromiseLike
interface, so you canawait
it; when aTask<T, E>
is awaited, it produces aResult<T, E>
.