Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Index

Type aliases

Functions

Type aliases

ErrorHandler

ErrorHandler<T>: Nullary<T> | Unary<T>

Function invoked if initial function throws an error.

param

the error thrown by x

returns

return value of trythen function

Type parameters

  • T

Nullary

Nullary<T>: () => T

Function invoked if initial function throws an error.

returns

return value of trythen function

Type parameters

  • T

Type declaration

    • (): T
    • Returns T

Unary

Unary<T>: (err: Error) => T

Function invoked if initial function throws an error.

param

the error thrown by x

returns

return value of trythen function

Type parameters

  • T

Type declaration

    • (err: Error): T
    • Parameters

      • err: Error

      Returns T

Functions

Export assignment trythen

  • If a function does not throw, returns the function return value; otherwise, returns the return value of a second function y.

    Notes

    • The function y is provided a single argument:

      • error: the error thrown by x

    Type parameters

    • T

    • U

    Parameters

    • x: () => T

      function to try invoking

        • (): T
        • Returns T

    • y: ErrorHandler<U>

      function to invoke if a function throws

    Returns T | U

    the return value of either x or y

    Example

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