Problem Statement
How would you perform async validations (e.g., username availability) without excessive requests?
Explanation
Use a watcher or watchEffect with a debounce (setTimeout/clearTimeout or a utility) to delay the API call until the user pauses typing. Invalidate stale requests with AbortController or by tracking the latest token.
Code Solution
SolutionRead Only
watch(() => form.username, (v, _, onInvalidate) => {
const ctrl = new AbortController()
const t = setTimeout(() => check(v, ctrl.signal), 300)
onInvalidate(() => { ctrl.abort(); clearTimeout(t) })
})