Skip to content

Instantly share code, notes, and snippets.

@antlis
Created April 16, 2026 20:18
Show Gist options
  • Select an option

  • Save antlis/379bae041b806a3c2fb612571ac8afef to your computer and use it in GitHub Desktop.

Select an option

Save antlis/379bae041b806a3c2fb612571ac8afef to your computer and use it in GitHub Desktop.
Cancels previous in-flight requests when a new one is triggered. Ideal for search inputs and rapid UI interactions.
export function useAbortableFetch() {
let controller: AbortController | null = null
return async function fetchWithAbort<T>(url: string, opts: any = {}) {
if (controller) controller.abort()
controller = new AbortController()
try {
return await $fetch<T>(url, {
...opts,
signal: controller.signal,
})
} finally {
controller = null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment