A Task is a type safe asynchronous computation.

You can think of a Task<T, E> as being basically a Promise<Result<T, E>>, because it is a Promise<Result<T, E>> under the hood, but with two main differences from a “normal” Promise:

  1. A Task cannot “reject”. All errors must be handled. This means that, like a Result, it will never throw an error if used in strict TypeScript.

  2. Unlike Promise, Task robustly distinguishes between map and andThen operations.

Task also implements JavaScript’s PromiseLike interface, so you can await it; when a Task<T, E> is awaited, it produces a Result<T, E>.

Constructors

  • 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.

    Type Parameters

    • T
    • E

    Returns Task

  • Produce a Task<T, E> from a promise of a Result<T, E>.

    Warning

    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.

    Type Parameters

    • T
    • E

    Parameters

    • promise: Promise<Result<T, E>>

      The promise from which to create the Task.

    Returns Task<T, E>

    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.

    Type Parameters

    • T = never
    • E extends {} = {}

    Returns Task<T, Unit>

  • 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.

    Type Parameters

    • T = never
    • E = unknown

    Parameters

    • reason: E

    Returns Task<T, E>

  • 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.

    Type Parameters

    • T extends Unit
    • E = never

    Returns Task<Unit, E>

  • 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.

    Type Parameters

    • T
    • E = never

    Parameters

    • value: T

    Returns Task<T, E>

  • 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.

    Type Parameters

    • T

    Parameters

    • promise: Promise<T>

      The promise from which to create the Task.

    Returns Task<T, unknown>

    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:

    • To leave any error as unknown, use the overload which accepts only the promise.
    • To handle the rejection reason rather than ignoring it, use the overload which accepts a function.

    Type Parameters

    • T
    • E

    Parameters

    • promise: Promise<T>

      The promise from which to create the Task.

    • rejectionValue: E

      A function to transform an unknown rejection reason into a known E.

    Returns Task<T, E>

    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.

    Type Parameters

    • T
    • E

    Parameters

    • promise: Promise<T>

      The promise from which to create the Task.

    • onRejection: (reason: unknown) => E

      A function to transform an unknown rejection reason into a known E.

    Returns Task<T, E>

    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

    Type Parameters

    • T
    • E

    Returns WithResolvers<T, E>

Properties

isPending: boolean
isRejected: boolean
isResolved: boolean
state: "Pending" | "Resolved" | "Rejected"

Methods

  • Type Parameters

    • U

      The type of the value for a resolved version of the other Task, i.e., the success type of the final Task present if the first Task is Ok.

    • F = E

    Parameters

    • other: Task<U, F>

      The Task instance to return if this is Rejected.

    Returns Task<U, E | F>

  • Type Parameters

    • U

      The type of the value produced by the new Task of the Result returned by the thenFn.

    • F = E

    Parameters

    • thenFn: (t: T) => Task<U, F>

      The function to apply to the wrapped T if maybe is Just.

    Returns Task<U, E | F>

  • Type Parameters

    • U

      The type of the resolved value of the returned Task.

    Parameters

    • mapFn: (t: T) => U

      The function to apply the value to when the Task finishes if it is Resolved.

    Returns Task<U, E>

  • Type Parameters

    • F

      The type of the rejection for the new Task, returned by the mapFn.

    Parameters

    • mapFn: (e: E) => F

      The function to apply to the rejection reason if the Task is rejected.

    Returns Task<T, F>

  • Type Parameters

    • A

    Parameters

    • matcher: Matcher<T, E, A>

      A lightweight object defining what to do in the case of each variant.

    Returns Promise<A>

  • Type Parameters

    • F

      The type wrapped in the Rejected case of other.

    • U = T

    Parameters

    • other: Task<U, F>

      The Result to use if this is Rejected.

    Returns Task<T | U, F>

    this if it is Resolved, otherwise other.

  • Type Parameters

    • F
    • U = T

    Parameters

    • elseFn: (reason: E) => Task<U, F>

      The function to apply to the Rejection reason if the Task rejects, to create a new Task.

    Returns Task<T | U, F>

  • Attaches callbacks for the resolution and/or rejection of the Promise.

    Type Parameters

    • A
    • B

    Parameters

    • OptionalonSuccess: (result: Result<T, E>) => A | PromiseLike<A>
    • OptionalonRejected: (reason: unknown) => B | PromiseLike<B>

    Returns PromiseLike<A | B>

    A Promise for the completion of which ever callback is executed.

  • Parameters

    • timerOrMs: number | Timer

      A Timer or a number of milliseconds to wait for this task before timing out.

    Returns Task<T, E | Timeout>

    A Task which has the resolution value of this or a Timeout if the timer elapsed.

  • Returns a string representation of an object.

    Returns string