Evercatch - v0.10.0
    Preparing search index...

    Variable fromReadonly

    from: <T, E extends unknown = Error>(
        fn: () => T,
        mapErr?: (err: unknown) => E,
    ) => Result<T, E> = resultFrom

    Type declaration

      • <T, E extends unknown = Error>(
            fn: () => T,
            mapErr?: (err: unknown) => E,
        ): Result<T, E>
      • Safely executes a function, catching any errors.

        Type Parameters

        • T

          The return type of the function.

        • E extends unknown = Error

          The type of the error.

        Parameters

        • fn: () => T

          The function to execute.

        • mapErr: (err: unknown) => E = defaultErrorMapper

          A function to map errors to a specific type.

        Returns Result<T, E>

        A Result containing either the function's return value or the caught error.

        const [error, data] = resultFrom(() => JSON.parse('{"foo": "bar"}'));
        if (error) {
        console.error(error.message);
        } else {
        console.log(data);
        }