NuxtN
Nuxt11mo ago
rd

Async function for `useAsyncData` key param

Hello, I have a composable that returns
useAsyncData
and want to do something like this

export const useSomeHook = () =>  {
  return useAsyncData(
    () => $fetch('some-endpoint-that-fetches-keys'),
    async () => {
      // do some fetch here
    }
  )
}


In other words, I want to set the key to some value fetched async, but if I make the composable async, the return type will always need to be a promise, which is not equivilent to
useAsyncData
's type signature:

function useAsyncData<DataT, DataE>(
  handler: (nuxtApp?: NuxtApp) => Promise<DataT>,
  options?: AsyncDataOptions<DataT>
): AsyncData<DataT, DataE>
function useAsyncData<DataT, DataE>(
  key: string,
  handler: (nuxtApp?: NuxtApp) => Promise<DataT>,
  options?: AsyncDataOptions<DataT>
): Promise<AsyncData<DataT, DataE>>


Can anyone think of a clean way to do this?
Was this page helpful?