Created
April 16, 2026 20:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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