Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "promise/try-catch"

Index

Functions

Functions

tryCatch

  • tryCatch<T, E>(promise: Promise<T>, defaultResult?: T): Promise<[E, T]>
  • Merges result and error in the same callback


    Example:

    import { tryCatch } from "@thalesrc/js-utils/promise";
    
    async function fooFunction() {
      const promise = anAsyncCall();
      const [error, result] = await tryCatch(promise);
    
      if (error) {
        // handle error
      }
    
      // do stuff
    }
    

    Prototype Example:

    import "@thalesrc/js-utils/promise/proto/try-catch";
    
    async function fooFunction() {
      const [error, result] = await anAsyncCall().tryCatch();
    
      if (error) {
        // handle error
      }
    
      // do stuff
    }
    

    Type parameters

    • T

    • E

    Parameters

    • promise: Promise<T>

      Promise to try

    • Default value defaultResult: T = null

      Setting this will put the value into the result field when the promise throws error

    Returns Promise<[E, T]>

    Error and result array

Generated using TypeDoc