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.
Produce 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.
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
.
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
.
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
.
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
.
Produce 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
.
Produce 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.
Produce 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.
Create 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
The public interface for the
Task
class as a value: a constructor and the associated static properties.