Evercatch - v0.9.0
    Preparing search index...

    Function unwrapAsyncOrElse

    • Unwraps a ResultAsync, computing a fallback value from the error when needed.

      Type Parameters

      • T

        The type of the value.

      • E = Error

        The type of the error.

      Parameters

      • resultAsync: ResultAsync<T, E>

        The ResultAsync to unwrap.

      • fallbackFn: (err: E) => T

        A function that receives the error and returns a fallback value.

      Returns Promise<T>

      A promise that resolves to the unwrapped value or the computed fallback.

      const value = await unwrapAsyncOrElse(Promise.resolve(ok(42)), () => 0);
      console.log(value); // 42
      const value = await unwrapAsyncOrElse(
      Promise.resolve(err(new Error("Oops"))),
      (error) => error.message.length,
      );
      console.log(value); // 4