@nevware21/ts-utils
    Preparing search index...

    Function safeGetDeferred

    • Create and return a readonly ICachedValue instance which will cache and return the value returned by the callback function. The callback function will only be called once, multiple access of the value will not cause re-execution of the callback as the result from the first call is cached internally. If the callback throws the default value will be returned. This is a lightweight version that does not support any expiration or invalidation.

      Type Parameters

      • T

        The type of the value to be cached

      • F extends (...args: any[]) => T = () => T

        The type of the callback function, defaults to () => T if not specified

      Parameters

      • cb: F

        The callback function to fetch the value to be lazily evaluated and cached

      • defValue: T

        The default value to return when an exception is thrown

      • OptionalargArray: Parameters<F>

        An optional array of arguments to be passed to the callback function

      Returns ICachedValue<T>

      A new readonly ICachedValue instance which wraps the callback and will be used to cache the result of the callback

      0.12.3

      // This does not cause the evaluation to occur
      let cachedValue = safeGetDeferred(
      () => JSON.parse(potentiallyInvalidJson),
      { defaultValue: true }
      );

      // With arguments
      let cachedValueWithArgs = safeGetDeferred(
      (id, name) => fetchDataThatMightFail(id, name),
      { defaultValue: "Not Found" },
      [123, "test"]
      );

      // This will cause the evaluation to occur and the result will be cached
      // If the evaluation throws, the default value will be returned
      let theValue = cachedValue.v;