import { tryCatch } from "https://deno.land/x/fun@v2.0.0/promise.ts";
Wrap a function that potentially throws in a try/catch block, handling any thrown errors and returning the result inside of a Promise.
Examples
Example 1
Example 1
import { tryCatch, reject, wrap } from "./promise.ts";
import { pipe, todo } from "./fn.ts";
// Note that todo will always throw synchronously.
const add = (n: number) => n + 1;
const throwSync = (_: number): number => todo();
const throwAsync = (_: number): Promise<number> => reject("Ha!");
const catchAdd = tryCatch(add, () => -1);
const catchSync = tryCatch(throwSync, () => -1);
const catchAsync = tryCatch(throwAsync, () => -1);
const resultAdd = await catchAdd(1); // 2
const resultSync = await catchSync(1); // -1
const resultAsync = await catchAsync(1); // -1