useAsyncValue

Source
import { useAsyncValue } from "@prestojs/util";
useAsyncValue(props)

Resolve a value from an id using an async function.

For the specified id the resolve function will be called and should return the value for the specified id.

If existingValues is provided this will be consulted first before calling resolve. This is useful when you have a list of values that may or may not have the data you care about. If it's there then the value will be returned immediately.

For multiple values see documentation below.

ParameterTypeDescription
props.existingValuesT[]

An optional array of existing values to try and find the value in.

If each item does not implement Identifiable then you must provide the getId function.

You can use this to avoid resolving data that already exists. If dealing with ViewModel instances you can use it with useViewModelCache, eg.

const existingValues = useViewModelCache(User, cache => cache.get(id, fieldNames));
const { value } = useAsyncValue({ id, existingValues, resolve: fetchUser });
props.getId
Function

A function that returns a unique ID for each item in existingValues. Only required if each item does not implement Identifiable.

props.onError
Function

Called when resolve errors. Passed the error returned from resolve.

props.onSuccess
Function

Called when resolve resolves successfully. Is passed a single parameter which is the value returned from resolve

props.trigger"MANUAL"|"DEEP"

When to trigger the fetch. Defaults to DEEP which means whenever id or ids changed it will refetch if the value hasn't already been resolved.

If set to MANUAL nothing will happen until it changes to DEEP. You can use this to defer execution until the value is required.

*props.idId|null

Single id for value to fetch or null if nothing yet to resolve.

If you need to resolve multiple values use the other form documented below passing ids instead.

*props.resolve
Function

Resolve the value for the provided ID. Function is passed a single parameter being id.

Note that when trigger is DEEP changes to this function will cause it to be called again so you must memoize it (eg. with useCallback) if it's defined in your component or hook.

UseAsyncValueReturn
KeyTypeDescription
errornull|Error

Set to the rejected value of the promise. Only one of error and value can be set. If isLoading is true consider this stale (ie. based on previous props). This can be useful when you want the UI to show the previous value until the next value is ready.

isLoadingboolean

True while resolve call is in progress.

reset
Function

When called will set both result and error to null. Will not immediately trigger a call to the action but subsequent changes to query or paginator will according to the value of trigger.

If value is found in existingValues then it will still be returned even if you call reset - reset only clears values that are returned from resolve.

run
Function

A function to manually trigger the action. If options.trigger is MANUAL calling this function is the only way to trigger the action.

This function will return a promise that resolves/rejects to same value returned by execute. If accumulatePages is set the value returned is the accumulated value.

valueT|null

The resolved value

useAsyncValue(props)

Resolve values from an array of ids using an async function.

For the specified array of ids the resolve function will be called and should return an array of the same size with each entry being the resolved value for the corresponding id.

If existingValues is provided this will be consulted first before calling resolve. This is useful when you have a list of values that may or may not have the data you care about. If it's there then the values will be returned immediately. Note that if any of the ids are missing from existingValues then it will be ignored and a call to resolve will be made requesting values for all ids.

ParameterTypeDescription
props.existingValuesT[]

An optional array of existing values to try and find the value in.

If each item does not implement Identifiable then you must provide the getId function.

You can use this to avoid resolving data that already exists. If dealing with ViewModel instances you can use it with useViewModelCache, eg.

const existingValues = useViewModelCache(User, cache => cache.get(id, fieldNames));
const { value } = useAsyncValue({ id, existingValues, resolve: fetchUser });
props.getId
Function

A function that returns a unique ID for each item in existingValues. Only required if each item does not implement Identifiable.

props.onError
Function

Called when resolve errors. Passed the error returned from resolve.

props.onSuccess
Function

Called when resolve resolves successfully. Is passed a single parameter which is the value returned from resolve

props.trigger"MANUAL"|"DEEP"

When to trigger the fetch. Defaults to DEEP which means whenever id or ids changed it will refetch if the value hasn't already been resolved.

If set to MANUAL nothing will happen until it changes to DEEP. You can use this to defer execution until the value is required.

*props.idsId[]|null

Array of ids to resolve values for or null if nothing yet to resolve

If you need to resolve a single value use the other form documented above passing id instead

*props.resolve
Function

Resolve the value for the provided IDs. Function is passed a single parameter being ids

UseAsyncValueReturn
KeyTypeDescription
errornull|Error

Set to the rejected value of the promise. Only one of error and value can be set. If isLoading is true consider this stale (ie. based on previous props). This can be useful when you want the UI to show the previous value until the next value is ready.

isLoadingboolean

True while resolve call is in progress.

reset
Function

When called will set both result and error to null. Will not immediately trigger a call to the action but subsequent changes to query or paginator will according to the value of trigger.

If value is found in existingValues then it will still be returned even if you call reset - reset only clears values that are returned from resolve.

run
Function

A function to manually trigger the action. If options.trigger is MANUAL calling this function is the only way to trigger the action.

This function will return a promise that resolves/rejects to same value returned by execute. If accumulatePages is set the value returned is the accumulated value.

valueT|null

The resolved value