Skip to content
Mosaic
On this page

useDebouncedAsync

Runs run(value, { signal }) once value has stopped changing for delayMs, and returns the latest result. The previous result stays while the next one is pending. A new value, disabling or unmounting aborts the signal and drops any late result.

Use for

  • a debounced, abortable async check as the user types
  • a password strength or availability check on an input

Practices

GuidancePractice
DoPass a primitive or memoized value. It is compared with Object.is, so an object built during render restarts the debounce every render.
DoForward signal to the request so a superseded check is cancelled.
Don’tDebounce with setTimeout in a feature. Use this hook instead.

Options

PropTypeDescription
delayMsnumberWait this long after the last change before running.
enabledbooleanWhen false, clears the result and cancels any waiting or running check.Default: true

Returns

PropTypeDescription
dataTDataThe result of the last settled run for the current or a previous value.
errorunknownWhat the last settled run rejected with.
isErrorbooleanWhether the last settled run rejected.
isPendingbooleanWhether no run has settled yet for the current value. Always false while disabled.

Examples

A password strength check

TSX
const strength = useDebouncedAsync(password, (value, { signal }) => checkStrength(value, { signal }), {
  delayMs: 300,
  enabled: password !== '',
});