Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "utils/try-catch/docs/types/index.d"

Index

Functions

Functions

Export assignment trycatch

  • trycatch<T, U>(x: () => T, y: U): T | U
  • If a function does not throw, returns the function return value; otherwise, returns y.

    Type parameters

    • T

    • U

    Parameters

    • x: () => T

      function to try invoking

        • (): T
        • Returns T

    • y: U

      value to return if a function throws

    Returns T | U

    either the return value of x or the provided argument y

    Example

    var randu = require( '@stdlib/random/base/randu' );
    
    function x() {
        if ( randu() < 0.5 ) {
            throw new Error( 'beep' );
        }
        return 1.0;
    }
    var z = trycatch( x, -1.0 );
    // returns <number>