Interface TaskConstructor

The public interface for the Task class as a value: a constructor and the associated static properties.

interface TaskConstructor {
    new TaskConstructor<T, E>(
        executor: (
            resolve: (value: T) => void,
            reject: (reason: E) => void,
        ) => void,
    ): Task<T, E>;
    prototype: TaskImpl<any, any>;
    fromResult<T, E>(result: Result<T, E>): Task<T, E>;
    fromUnsafePromise<T, E>(promise: Promise<Result<T, E>>): Task<T, E>;
    reject<T = never, E extends {} = {}>(): Task<T, Unit>;
    reject<T = never, E = unknown>(reason: E): Task<T, E>;
    resolve<T extends Unit, E = never>(): Task<Unit, E>;
    resolve<T, E = never>(value: T): Task<T, E>;
    try<T>(promise: Promise<T>): Task<T, unknown>;
    tryOr<T, E>(promise: Promise<T>, rejectionValue: E): Task<T, E>;
    tryOrElse<T, E>(
        promise: Promise<T>,
        onRejection: (reason: unknown) => E,
    ): Task<T, E>;
    withResolvers<T, E>(): WithResolvers<T, E>;
}

Hierarchy

  • Omit<typeof TaskImpl, "constructor">
    • TaskConstructor

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

    Parameters

    • executor: (resolve: (value: T) => void, reject: (reason: E) => void) => void

      A function which the constructor will execute to manage the lifecycle of the Task. The executor in turn has two functions as parameters: one to call on resolution, the other on rejection.

    Returns Task<T, E>

  • 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

prototype: TaskImpl<any, any>

Methods