Get Data with fetch()

February 17th, 2023

1const joke = ref(null)
2 
3function loadJoke() {
4 fetch('https://v2.jokeapi.dev/joke/christmas')
5 .then(response => response.json())
6 .then(data => (joke.value = data))
7}

async...await, try...catch:

1const setup = ref(null)
2const delivery = ref(null)
3 
4const loadJoke = async () => {
5 try {
6 const data = await fetch('https://v2.jokeapi.dev/joke/christmas')
7 .then(response => response.json())
8 
9 setup.value = data.setup
10 delivery.value = data.delivery
11 } catch (error) {
12 console.error(error)
13 alert('Something went wrong!')
14 }
15}

Related Posts