From fff45324caf139f8cf6fdee320e61951df9b81c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Sat, 4 Mar 2023 20:53:02 +0800 Subject: [PATCH] fix(useFetch): abort before updating state (#2805) --- packages/core/useFetch/index.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/core/useFetch/index.ts b/packages/core/useFetch/index.ts index 2084d33245b..4bfc40a0525 100644 --- a/packages/core/useFetch/index.ts +++ b/packages/core/useFetch/index.ts @@ -357,9 +357,14 @@ export function useFetch(url: MaybeComputedRef, ...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, + } } } @@ -372,21 +377,13 @@ export function useFetch(url: MaybeComputedRef, ...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: {},