Skip to content

What is the difference between useFetch and useAsyncData?? #18193

Discussion options

You must be logged in to vote

useAsyncData accepts any function that returns a promise.

You might understand it better with an example.

Say you want to fetch todos using useAsyncData:

<script setup lang="ts">
// todos here can be whatever. It's a unique key to ensure that data fetching can be properly de-duplicated across requests.
const { data } = useAsyncData('todos', () =>  $fetch('https://jsonplaceholder.typicode.com/todos'))
</script>

and using useFetch:

<script setup lang="ts">
// a key is not required as it automatically generates a key based on url.
const { data } = useFetch('https://jsonplaceholder.typicode.com/todos')
</script>

Replies: 0 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@harblaith7
Comment options

@wobsoriano
Comment options

Answer selected by harblaith7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants