Skip to content

Commit

Permalink
fix(useFetch): abort before updating state (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Mar 4, 2023
1 parent ef281cd commit fff4532
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions packages/core/useFetch/index.ts
Expand Up @@ -357,9 +357,14 @@ export function useFetch<T>(url: MaybeComputedRef<string>, ...args: any[]): UseF
let timer: Stoppable | undefined

const abort = () => {
if (supportsAbort && controller) {
controller.abort()
controller = undefined
if (supportsAbort) {
controller?.abort()
controller = new AbortController()
controller.signal.onabort = () => aborted.value = true
fetchOptions = {
...fetchOptions,
signal: controller.signal,
}
}
}

Expand All @@ -372,21 +377,13 @@ export function useFetch<T>(url: MaybeComputedRef<string>, ...args: any[]): UseF
timer = useTimeoutFn(abort, timeout, { immediate: false })

const execute = async (throwOnFailed = false) => {
abort()

loading(true)
error.value = null
statusCode.value = null
aborted.value = false

if (supportsAbort) {
abort()
controller = new AbortController()
controller.signal.onabort = () => aborted.value = true
fetchOptions = {
...fetchOptions,
signal: controller.signal,
}
}

const defaultFetchOptions: RequestInit = {
method: config.method,
headers: {},
Expand Down

0 comments on commit fff4532

Please sign in to comment.