Skip to content
Snippets Groups Projects
Commit 26a50d64 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

fix: fix useAsyncFunction implementation

As we wrap an async function we must await it. Otherwise finally is
called immediately.
parent 0167528b
No related branches found
No related tags found
No related merge requests found
...@@ -13,14 +13,12 @@ export const useId = (() => { ...@@ -13,14 +13,12 @@ export const useId = (() => {
} }
})() })()
export function useAsyncFunction<F extends (...args: never[]) => ReturnType<F>>( export function useAsyncFunction<F extends (...args: never[]) => Promise<unknown>>(fn: F) {
fn: F,
): { isLoading: Ref<boolean>; fn: (...args: Parameters<F>) => ReturnType<F> } {
const isLoading = ref(false) const isLoading = ref(false)
function wrapper(...args: Parameters<F>): ReturnType<F> { async function wrapper(...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> {
isLoading.value = true isLoading.value = true
try { try {
return fn(...args) return (await fn(...args)) as Awaited<ReturnType<F>>
} finally { } finally {
isLoading.value = false isLoading.value = false
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment