@allynet/ishod
    Preparing search index...

    Function try$

    • Try to run a function and return a result.

      If the function throws an error, it will be caught and returned as an Err.

      Type Parameters

      • const T extends Primitive
      • E

      Parameters

      • fn: () => T

      Returns Result<T, E>

      const result = try$(() => {
      return 1;
      });
      // result is an Ok with the value 1;
      const result = try$(() => {
      throw new Error("error");
      });
      // result is an Err with the error "error";
      const result: Promise<Result<number, string>> = try$(Promise.resolve(1));
      // result is an Ok with the value 1;
      const result: Promise<Result<number, string>> = try$(Promise.reject("error"));
      // result is an Err with the error "error";
    • Try to run a function and return a result.

      If the function throws an error, it will be caught and returned as an Err.

      Type Parameters

      • T
      • E

      Parameters

      • fn: Promise<T>

      Returns Promise<Result<T, E>>

      const result = try$(() => {
      return 1;
      });
      // result is an Ok with the value 1;
      const result = try$(() => {
      throw new Error("error");
      });
      // result is an Err with the error "error";
      const result: Promise<Result<number, string>> = try$(Promise.resolve(1));
      // result is an Ok with the value 1;
      const result: Promise<Result<number, string>> = try$(Promise.reject("error"));
      // result is an Err with the error "error";
    • Try to run a function and return a result.

      If the function throws an error, it will be caught and returned as an Err.

      Type Parameters

      • T
      • E

      Parameters

      • fn: () => Promise<T>

      Returns Promise<Result<T, E>>

      const result = try$(() => {
      return 1;
      });
      // result is an Ok with the value 1;
      const result = try$(() => {
      throw new Error("error");
      });
      // result is an Err with the error "error";
      const result: Promise<Result<number, string>> = try$(Promise.resolve(1));
      // result is an Ok with the value 1;
      const result: Promise<Result<number, string>> = try$(Promise.reject("error"));
      // result is an Err with the error "error";
    • Try to run a function and return a result.

      If the function throws an error, it will be caught and returned as an Err.

      Type Parameters

      • T
      • E

      Parameters

      • fn: () => T

      Returns Result<T, E>

      const result = try$(() => {
      return 1;
      });
      // result is an Ok with the value 1;
      const result = try$(() => {
      throw new Error("error");
      });
      // result is an Err with the error "error";
      const result: Promise<Result<number, string>> = try$(Promise.resolve(1));
      // result is an Ok with the value 1;
      const result: Promise<Result<number, string>> = try$(Promise.reject("error"));
      // result is an Err with the error "error";