@adviser/cement - v0.0.0
    Preparing search index...

    Function exception2Result

    • Wraps a function to convert thrown exceptions into Result.Err values.

      Executes the provided function and returns Result.Ok on success or Result.Err on thrown exceptions. Supports both synchronous and asynchronous functions.

      Type Parameters

      • FN extends () => T | Promise<T>

        Function type that returns T or Promise

      • T

        The return type of the function

      Parameters

      • fn: FN

        Function to execute with exception handling

      Returns WithResult<ReturnType<FN>>

      Result for sync functions, Promise<Result> for async functions

      const result = exception2Result(() => {
      return JSON.parse('invalid json');
      });
      // result is Result.Err with parse error

      const asyncResult = await exception2Result(async () => {
      return await fetch('/api/data');
      });