Skip to content

Instantly share code, notes, and snippets.

@telegraham
Last active May 17, 2019 18:16
Show Gist options
  • Save telegraham/e39e55fdaff60a790daed2ee766742c1 to your computer and use it in GitHub Desktop.
Save telegraham/e39e55fdaff60a790daed2ee766742c1 to your computer and use it in GitHub Desktop.

Fetch Quiz

The quiz questions relate to this piece of code:

fetch("http://www.ajax.com/dinosaurs/13", {
  method: "PATCH",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
  body: JSON.stringify({
    species: "Tyrannosaurus Rex",
    name: "SUE"
  })
}).then(response => response.json())
  .then(data => slapItOnTheDom(data))

Part I

  fetch("http://www.ajax.com/dinosaurs/13", {
    method: "PATCH",
  1. What does the string passed in as the first parameter to fetch do?
  2. What does the method key of the second parameter do?
  3. Based on the values of the string, the value of the method key, and the REST standard, what will this fetch do in the API?

Part II

  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
  1. What is the "Content-Type" header for?
  2. What is the "Accept" header for?
  3. When do I need a "Content-Type" header?
  4. When do I need an "Accept" header?

Part III

  body: JSON.stringify({
    species: "Tyrannosaurus Rex",
    name: "SUE"
  })
  1. What data type does the value of the body key need to be?
  2. What will the return value of JSON.stringify look like, based on the parameter shown here?
  3. What purpose does the body of a PATCH request serve?
  4. Should there be an ID in the object being passed to JSON.stringify? Why?

Part IV

}).then(response => response.json())
  1. What are we calling .then on?
  2. What is being passed to the .then method?
  3. What is response?
  4. What does .json() do? What does it return?

Part V

  .then(data => slapItOnTheDom(data))
  1. What is data?
  2. What does slapItOnTheDom probably do with data?
  3. Is this optimistic rendering, pessimistic rendering, or neither?

Bonus

  1. What does it mean that fetch is asynchronous?
  2. Why does JavaScript use asynchronous code so much?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment