Function fromResult

  • Build a Task<T, E> from a Result<T, E>.

    Important

    This does not (and by definition cannot) handle errors that happen during construction of the Result, because those happen before this is called. See tryOr and tryOrElse as well as the corresponding Result.tryOr and Result.tryOrElse methods for synchronous functions.

    Given an Ok, fromResult will produces a Resolved task.

    import { fromResult } from 'true-myth/task';
    import { ok } from 'true-myth/result';

    let successful = fromResult(ok("hello")); // -> Resolved("hello")

    Likewise, given an Err, fromResult will produces a Rejected task.

    import { fromResult } from 'true-myth/task';
    import { err } from 'true-myth/result';

    let successful = fromResult(err("uh oh!")); // -> Rejected("uh oh!")

    It is often clearest to access the function via a namespace-style import:


    import * as Task from 'true-myth/task';
    import { ok } from 'true-myth/result';

    let theTask = Task.fromResult(ok(123));

    As an alternative, it can be useful to rename the import:

    import { fromResult: taskFromResult } from 'true-myth/task';
    import { err } from 'true-myth/result';

    let theTask = taskFromResult(err("oh no!"));

    Type Parameters

    • T
    • E

    Parameters

    Returns Task<T, E>