Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/fn/fn_async_either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as F from "../../fn.ts";
import * as FAE from "../../fn_async_either.ts";

const add1 = await F.pipe(
FAE.ask<number>(),
FAE.map((n) => n + 1),
FAE.match(() => 0, F.identity),
);

add1(2).then((n) => console.log(n)); // 3

const failable = F.pipe(
FAE.tryCatch(
(n: number) => n % 2 === 0 ? Promise.resolve("even") : Promise.reject("odd"),
() => "fail: odd",
),
FAE.match(() => "fail: even", F.identity),
);

failable(1).then((n) => console.log(n)); // odd;

const askN = FAE.ask<number>();

const divide = ([dividend, divisor]: [number, number]) => dividend / divisor;
const onError = (error: unknown, [[dividend, divisor]]: [[number, number]]) =>
`Error dividing ${dividend} by ${divisor}`;
const safeDivide = FAE.tryCatch(divide, onError);

safeDivide([10, 2]); // returns Right(5)
safeDivide([10, 0]); // returns Left("Error dividing 10 by 0")

const _fetch = FAE.tryCatch(
fetch,
(error, args) => ({ message: "Fetch Error", error, args }),
);

const t1 = await _fetch("blah")();
const t2 = await _fetch("https://deno.land/")();

const computation = FAE.left<number, number, number>(1);
const result = await computation()();
Loading