Skip to content

Commit

Permalink
fix: clear timeout when asyncData request finishes and aborts (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx authored May 23, 2024
1 parent ba491a7 commit 61e7f46
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/bridge/src/runtime/composables/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ export function useFetch<
* @see https://github.com/unjs/ofetch/blob/bb2d72baa5d3f332a2185c20fc04e35d2c3e258d/src/fetch.ts#L152
*/
const timeoutLength = toValue(opts.timeout)
let timeoutId: NodeJS.Timeout
if (timeoutLength) {
setTimeout(() => controller.abort(), timeoutLength)
timeoutId = setTimeout(() => controller.abort(), timeoutLength)
controller.signal.onabort = () => clearTimeout(timeoutId)
}

let _$fetch = opts.$fetch || globalThis.$fetch
Expand All @@ -158,7 +160,7 @@ export function useFetch<
}
}

return _$fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any) as Promise<_ResT>
return _$fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any).finally(() => { clearTimeout(timeoutId) }) as Promise<_ResT>
}, _asyncDataOptions)

return asyncData
Expand Down

0 comments on commit 61e7f46

Please sign in to comment.