Function valueOrProviderResult

  • Resolves a value that was provided via ValueOrProvider

    Can be called with optional args which will be used as arguments when calling the Provider Function.

    Example

    Create a Function that accepts a value that can be either lazy and eager inizialized

    function fn(value: ValueOrProvider<string>) {
    // ... some code that may return early from the Function ...

    // resolve the value when it is actually needed
    const actualValue = valueOrProviderResult(value);
    console.log(typeof actualValue); // 'string'
    }

    Call the Function with an eager initialized value

    fn(1);
    

    Call the Function with a lazy initialized value

    function someExpensiveOperation() { /* ... */ }

    fn(someExpensiveOperation);

    Example

    Create a Function that accepts a value that can be either static or dynamic

    function add(numbers: number[], value: ValueOrProvider<number, [number]>) {
    return numbers.map(n => n + valueOrProviderResult(value, e));
    }

    Call the Function with a static value

    const a = add([1,2,3], 1);
    console.log(a); // [2,3,4]

    Call the Function with a dynamic value

    const a = add([1,2,3], n => 2+n);
    console.log(a); // [4,6,8]

    Returns

    the result of the Function if value is a Function, otherwise returns value itself

    Type Parameters

    • T

    • TArgs extends unknown[] = []

    Parameters

    Returns T

Generated using TypeDoc