2022-10-06 09:15:30 +00:00
---
description: useAsyncData provides access to data that resolves asynchronously.
---
2022-04-06 05:56:08 +00:00
# `useAsyncData`
2022-04-11 17:24:18 +00:00
Within your pages, components, and plugins you can use useAsyncData to get access to data that resolves asynchronously.
2022-04-06 05:56:08 +00:00
2023-05-30 09:05:41 +00:00
::alert{type=warning}
`useAsyncData` is a composable meant to be called directly in a setup function, plugin, or route middleware. It returns reactive composables and handles adding responses to the Nuxt payload so they can be passed from server to client without re-fetching the data on client side when the page hydrates.
::
2022-04-11 17:24:18 +00:00
## Type
```ts [Signature]
2022-07-07 16:26:04 +00:00
function useAsyncData(
handler: (nuxtApp?: NuxtApp) => Promise< DataT > ,
options?: AsyncDataOptions< DataT >
): AsyncData< DataT >
2022-04-11 17:24:18 +00:00
function useAsyncData(
2022-04-06 05:56:08 +00:00
key: string,
2022-04-11 17:24:18 +00:00
handler: (nuxtApp?: NuxtApp) => Promise< DataT > ,
2022-07-07 16:26:04 +00:00
options?: AsyncDataOptions< DataT >
): Promise< AsyncData < DataT > >
2022-04-11 17:24:18 +00:00
2022-07-07 16:26:04 +00:00
type AsyncDataOptions< DataT > = {
2022-04-11 17:24:18 +00:00
server?: boolean
lazy?: boolean
2022-09-05 11:43:40 +00:00
default?: () => DataT | Ref< DataT > | null
2022-04-11 17:24:18 +00:00
transform?: (input: DataT) => DataT
pick?: string[]
watch?: WatchSource[]
2022-09-07 09:47:40 +00:00
immediate?: boolean
2022-04-11 17:24:18 +00:00
}
2022-09-05 11:43:40 +00:00
type AsyncData< DataT , ErrorT > = {
data: Ref< DataT | null >
2022-04-11 17:24:18 +00:00
pending: Ref< boolean >
2023-05-18 18:54:06 +00:00
refresh: (opts?: AsyncDataExecuteOptions) => Promise< void >
execute: (opts?: AsyncDataExecuteOptions) => Promise< void >
2022-09-05 11:43:40 +00:00
error: Ref< ErrorT | null >
2023-06-09 21:38:14 +00:00
status: Ref< AsyncDataRequestStatus >
2023-05-18 18:54:06 +00:00
};
2022-09-05 11:43:40 +00:00
2023-05-18 18:54:06 +00:00
interface AsyncDataExecuteOptions {
dedupe?: boolean
}
2023-06-09 21:38:14 +00:00
type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
2022-04-06 05:56:08 +00:00
```
## Params
2022-07-07 16:26:04 +00:00
* **key**: a unique key to ensure that data fetching can be properly de-duplicated across requests. If you do not provide a key, then a key that is unique to the file name and line number of the instance of `useAsyncData` will be generated for you.
2022-04-06 05:56:08 +00:00
* **handler**: an asynchronous function that returns a value
* **options**:
2022-09-03 11:02:21 +00:00
* _lazy_: whether to resolve the async function after loading the route, instead of blocking client-side navigation (defaults to `false` )
2022-04-06 05:56:08 +00:00
* _default_: a factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option
2022-04-16 13:53:36 +00:00
* _server_: whether to fetch the data on the server (defaults to `true` )
2022-04-06 05:56:08 +00:00
* _transform_: a function that can be used to alter `handler` function result after resolving
2022-04-16 13:53:36 +00:00
* _pick_: only pick specified keys in this array from the `handler` function result
* _watch_: watch reactive sources to auto-refresh
2022-09-07 09:47:40 +00:00
* _immediate_: When set to `false` , will prevent the request from firing immediately. (defaults to `true` )
2022-04-06 05:56:08 +00:00
Under the hood, `lazy: false` uses `<Suspense>` to block the loading of the route before the data has been fetched. Consider using `lazy: true` and implementing a loading state instead for a snappier user experience.
2022-08-13 07:27:04 +00:00
## Return Values
2022-04-06 05:56:08 +00:00
* **data**: the result of the asynchronous function that is passed in
* **pending**: a boolean indicating whether the data is still being fetched
2022-09-07 09:47:40 +00:00
* **refresh**/**execute**: a function that can be used to refresh the data returned by the `handler` function
2022-04-06 05:56:08 +00:00
* **error**: an error object if the data fetching failed
2023-06-09 21:38:14 +00:00
* **status**: a string indicating the status of the data request (`"idle"`, `"pending"` , `"success"` , `"error"` )
2022-04-06 05:56:08 +00:00
2022-09-03 11:02:21 +00:00
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
::alert{type=warning}
If you have not fetched data on the server (for example, with `server: false` ), then the data _will not_ be fetched until hydration completes. This means even if you await `useAsyncData` on the client side, `data` will remain `null` within `<script setup>` .
::
2022-04-11 17:24:18 +00:00
## Example
```ts
2022-04-25 09:31:25 +00:00
const { data, pending, error, refresh } = await useAsyncData(
2022-04-11 17:24:18 +00:00
'mountains',
2022-04-25 09:31:25 +00:00
() => $fetch('https://api.nuxtjs.dev/mountains')
2022-04-11 17:24:18 +00:00
)
```
2023-03-19 23:51:18 +00:00
## Example with watching params change
The built-in `watch` option allows automatically rerunning the fetcher function when any changes are detected.
```ts
const page = ref(1)
const { data: posts } = await useAsyncData(
'posts',
() => $fetch('https://fakeApi.com/posts', {
params: {
page: page.value
}
}), {
watch: [page]
}
)
```
2023-02-03 11:55:58 +00:00
::alert{type=warning}
`useAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useAsyncData` .
::
2022-11-16 10:04:28 +00:00
::ReadMore{link="/docs/getting-started/data-fetching"}
2022-04-11 17:24:18 +00:00
::