Skip to content

Commit 44c0ba8

Browse files
authored
feat(useFetch): support for custom abort reason (#4820)
1 parent 6f56583 commit 44c0ba8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/core/useFetch/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,4 +835,19 @@ describe.skipIf(isBelowNode18)('useFetch', () => {
835835
expect(data.value).toEqual(expect.objectContaining({ after: 'Global' }))
836836
})
837837
})
838+
839+
it('should abort with given reason', async () => {
840+
const { aborted, abort, execute, onFetchError } = useFetch('https://example.com', { immediate: false })
841+
const reason = 'custom abort reason'
842+
let error: unknown
843+
onFetchError((err) => {
844+
error = err
845+
})
846+
execute()
847+
abort(reason)
848+
await vi.waitFor(() => {
849+
expect(aborted.value).toBe(true)
850+
expect(error).toBe(reason)
851+
})
852+
})
838853
})

packages/core/useFetch/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface UseFetchReturn<T> {
4848
/**
4949
* Abort the fetch request
5050
*/
51-
abort: Fn
51+
abort: (reason?: any) => void
5252

5353
/**
5454
* Manually call the fetch
@@ -394,9 +394,9 @@ export function useFetch<T>(url: MaybeRefOrGetter<string>, ...args: any[]): UseF
394394
let controller: AbortController | undefined
395395
let timer: Stoppable | undefined
396396

397-
const abort = () => {
397+
const abort = (reason?: any) => {
398398
if (supportsAbort) {
399-
controller?.abort()
399+
controller?.abort(reason)
400400
controller = new AbortController()
401401
controller.signal.onabort = () => aborted.value = true
402402
fetchOptions = {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy