@lou.codes/parsers
Version:
👁️🗨️ Parsers without nonsense
26 lines (25 loc) • 813 B
TypeScript
import type { Maybe, ReadOnlyArray } from "@lou.codes/types";
/**
* Wrapper for `try`/`catch`.
*
* @category Common
* @remarks
* This functions tries to run a function and silences the `throw`s by wrapping
* it with a `try/catch` and returning `undefined` instead.
* @example
* ```typescript
* const willFail = () => {
* throw new Error("fail");
* };
*
* const safeWillFail = attempt(willFail);
* safeWillFail(); // undefined
* ```
*
* @template Arguments Type of the arguments of the function to be wrapped.
* @param wrappedFunction Function to be wrapped.
* @returns Function wrapped in `try`/`catch`.
*/
export declare const attempt: <Arguments extends ReadOnlyArray, Output>(
wrappedFunction: (...wrappedArguments: Arguments) => Output,
) => (...parameters: Arguments) => Maybe<Output>;