Skip to content

Latest commit

 

History

History

Dad Jokes

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Dad Jokes Project

HTML Structure

  • div.container
    • button.btn
    • p.result(dummy text)

External Data

  • the main ides is the same, just external data

What is an API?

What is an API?

Dad Jokes Docs

Select Elements

  • select btn, result
  • check if both elements selected
  • listen for click events

FetchDadJoke Function

  • create async function
  • setup fetch
  • set result.textContent = joke
const fetchDadJoke = async () => {
  const response = await fetch(url, {
    headers: {
      Accept: "application/json",
      "User-Agent": "learning app",
    },
  });

  const data = await response.json();
  result.textContent = data.joke;
};

Loading

  • set result equal to - "Loading..."

Error Handling

  • try/catch block
  • set result equal to - "There was an error..."

Check Status

  • Fetch - only throws an error if cannot resolve
  • Error response still a response
  • check for status
  • throw new Error("Whoops!")