• Awaits a promise and returns a loading value while the promise is unresolved

    Type Parameters

    • T

    Parameters

    • promiseFactoryCallback: undefined | (() => Promise<T>)

      A function that returns the promise to await. If this callback is undefined, the current value will be returned (defaultValue unless it was previously changed and options.preserveValue is true), and there will be no loading.

      WARNING: MUST BE STABLE - const or wrapped in useCallback. The reference must not be updated every render

    • defaultValue: T

      The initial value to return while first awaiting the promise. If options.preserveValue is false, this value is also shown while awaiting the promise on subsequent calls.

      Note: this parameter is internally assigned to a ref, so changing it will not cause any hooks to re-run with its new value. This means that, if the promiseFactoryCallback changes and options.preserveValue is false, the returned value will be set to the current defaultValue. However, the returned value will not be updated ifdefaultValue changes.

    • options: UsePromiseOptions = {}

      Various options for adjusting how this hook runs the promiseFactoryCallback

      Note: this parameter is internally assigned to a ref, so changing it will not cause any hooks to re-run with its new value. However, the latest options.preserveValue will always be used appropriately to determine whether to preserve the returned value when changing the promiseFactoryCallback

    Returns [value: T, isLoading: boolean]

    [value, isLoading]

    • value: the current value for the promise, either the defaultValue or the resolved promise value
    • isLoading: whether the promise is waiting to be resolved