Skip to content

True Myth / task / fromResult

Function: fromResult()

fromResult<T, E>(result): Task<T, E>

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.

Examples

Given an Ok<T, E>, fromResult will produces a Resolved<T, E> task.

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

ts
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:

ts

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:

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

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

Type Parameters

T

T

E

E

Parameters

result

Result<T, E>

Returns

Task<T, E>