True Myth / task / safelyTryOr
Variable: safelyTryOr()
const
safelyTryOr: {<T
,E
>(rejection
,fn
):Task
<T
,E
>; <T
,E
>(rejection
): (fn
) =>Task
<T
,E
>; } =tryOr
An alias for tryOr
for ease of migrating from v8.x to v9.x.
TIP
You should switch to tryOr
. We expect to deprecate and remove this alias at some point!
Call Signature
<
T
,E
>(rejection
,fn
):Task
<T
,E
>
Given a function which takes no arguments and returns a Promise
and a value of type E
to use as the rejection if the Promise
rejects, return a Task<T, E>
for the result of invoking that function. This safely handles functions which fail synchronously or asynchronously, so unlike fromPromise
is safe to use with values which may throw errors before producing a Promise
.
Examples
import { tryOr } from 'true-myth/task';
function throws(): Promise<number> {
throw new Error("Uh oh!");
}
// Note: passing the function by name, *not* calling it.
let theTask = tryOr("fallback", throws);
let theResult = await theTask;
if (theResult.isErr) {
console.error(theResult.error); // "fallback"
}
You can also write this in “curried” form, passing just the fallback value and getting back a function which accepts the:
import { tryOr } from 'true-myth/task';
function throws(): Promise<number> {
throw new Error("Uh oh!");
}
// Note: passing the function by name, *not* calling it.
let withFallback = tryOr<number, string>("fallback");
let theResult = await withFallback(throws);
if (theResult.isErr) {
console.error(theResult.error); // "fallback"
}
Note that in the curried form, you must specify the expected T
type of the resulting Task
, or else it will always be unknown
.
Type Parameters
T
T
E
E
Parameters
rejection
E
The value to use if the Promise
rejects.
fn
() => Promise
<T
>
A function which returns a Promise
when called.
Returns
Task
<T
, E
>
A Task
which resolves to the resolution value of the promise or rejects with the rejection value of the promise or any error thrown while invoking fn
.
Call Signature
<
T
,E
>(rejection
): (fn
) =>Task
<T
,E
>
Given a function which takes no arguments and returns a Promise
and a value of type E
to use as the rejection if the Promise
rejects, return a Task<T, E>
for the result of invoking that function. This safely handles functions which fail synchronously or asynchronously, so unlike fromPromise
is safe to use with values which may throw errors before producing a Promise
.
Examples
import { tryOr } from 'true-myth/task';
function throws(): Promise<number> {
throw new Error("Uh oh!");
}
// Note: passing the function by name, *not* calling it.
let theTask = tryOr("fallback", throws);
let theResult = await theTask;
if (theResult.isErr) {
console.error(theResult.error); // "fallback"
}
You can also write this in “curried” form, passing just the fallback value and getting back a function which accepts the:
import { tryOr } from 'true-myth/task';
function throws(): Promise<number> {
throw new Error("Uh oh!");
}
// Note: passing the function by name, *not* calling it.
let withFallback = tryOr<number, string>("fallback");
let theResult = await withFallback(throws);
if (theResult.isErr) {
console.error(theResult.error); // "fallback"
}
Note that in the curried form, you must specify the expected T
type of the resulting Task
, or else it will always be unknown
.
Type Parameters
T
T
E
E
Parameters
rejection
E
The value to use if the Promise
rejects.
Returns
A Task
which resolves to the resolution value of the promise or rejects with the rejection value of the promise or any error thrown while invoking fn
.
(
fn
):Task
<T
,E
>
Parameters
fn
() => Promise
<T
>
Returns
Task
<T
, E
>