Skip to content

Instantly share code, notes, and snippets.

@jeremiahblackol
Forked from khalidwilliams/promises_review.md
Last active July 1, 2020 16:55
Show Gist options
  • Save jeremiahblackol/4a47a426f431b5d59091ceb06503b447 to your computer and use it in GitHub Desktop.
Save jeremiahblackol/4a47a426f431b5d59091ceb06503b447 to your computer and use it in GitHub Desktop.

Take 10 minutes to research the following in breakout groups. When you get back, I'm gonna call on some people to answer and explain.

1. What does the fetch api do?
Requests data from an api
Allows us to get or modify data from an api

2. What does .fetch() return?
It returns a promise
A promise in a JS object that represents the eventual completion of an action

3. What does .then() do? What is the method called on? What does it return?
Parses the data and sorts it. It is called on the promise.
Method called on a promise, runs when Promise is returned successfully(resolved); returns a new promise

4. What does .catch() do? What is the method called on? What does it return?
Handles errors for failed network requests.
Called on a promise, also returns a promise

And tackle this diagram:

someAsyncFunction() // this will return a promise.
	.then( (1) ) // runs when promise is successful
	.then( (2) ) // after the first .then() - if second promise is successful
	.catch( (3) ) // runs if a promise is not returned( if request is failed or rejected )
// What goes in place of (1), (2), and (3)?
1. response => response.json()
2. data => whatever you want to do with the data
3. err => console.log(err.message)

1, 2 and 3 are all callback functions
the callbacks all evaluate to some value.
that value becomes the resolved value of .then / or .catch returns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment