Function attemptOrElse

  • Attempt to call a function or call a handler if an error occurs.

    Returns

    the result of fnRes if succesfull or the result of fnErr

    Note

    the type of the return is always the same regardless of if an error occurs

    Example

    attemptOrElse(() => { throw new Error() }, (err) => 'oh no') // returns 'oh no'
    

    Example

    attemptOrElse(() => 'hello', (err) => 'oh no') === 'hello'
    

    Example

    const isDebugEnv = attemptOrElse(() => process.env.NODE_ENV === 'debug', () => false)
    

    Type Parameters

    • TReturn

    • TError extends Error

    Parameters

    • fnRes: (() => TReturn)

      the function to call

        • (): TReturn
        • Returns TReturn

    • fnErr: ((err) => TReturn)

      the handler to call if an error occurs

        • (err): TReturn
        • Parameters

          • err: TError

          Returns TReturn

    Returns TReturn

Generated using TypeDoc