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 = (() => {
}
})()
export function useAsyncFunction<F extends (...args: never[]) => ReturnType<F>>(
fn: F,
): { isLoading: Ref<boolean>; fn: (...args: Parameters<F>) => ReturnType<F> } {
export function useAsyncFunction<F extends (...args: never[]) => Promise<unknown>>(fn: F) {
const isLoading = ref(false)
function wrapper(...args: Parameters<F>): ReturnType<F> {
async function wrapper(...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> {
isLoading.value = true
try {
return fn(...args)
return (await fn(...args)) as Awaited<ReturnType<F>>
} finally {
isLoading.value = false
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment