Problem Statement
Describe an approach for asynchronous field validation (e.g., unique email).
Explanation
Debounce user input, call an API, then set an error state on the field based on the response. Cancel stale requests via AbortController or a request token to avoid race conditions, and show inline feedback and a neutral state while checking.
Code Solution
SolutionRead Only
let ctrl: AbortController | null = null
async function checkEmail(email:string){
ctrl?.abort(); ctrl = new AbortController()
const res = await fetch(`/api/exists?email=${email}`, { signal: ctrl.signal })
state.emailError = (await res.json()).exists ? 'Email already used' : ''
}